Re: NameError: when calling lambda from irb

Raj Sahae <[email protected]> Sat, 7 Dec 2019 01:49:18 -0800
Newsgroups gmane.comp.lang.ruby.general
Message-ID <CAJHnM_4NxdONLOagO6B9cusMzz79MLOY2jr71gM+FMEf7wHkbg@mail.gmail.com>
--===============0043678004==
Content-Type: multipart/alternative; boundary="0000000000001e9dbf05991a14b0"

--0000000000001e9dbf05991a14b0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

You are right, it's not the load path. I'm not sure exactly why but the
variables holding the procs are maybe going out of scope? If you make them
globals, constants, or methods, then it works.

=E2=9D=AF cat lambda.rb
#!/usr/bin/env ruby

$new_db =3D lambda do
  db =3D{}
  insert =3D lambda do |artist, album|
    db[artist] =3Dalbum
  end
  {insert: insert}
end

SOME_LAMBDA =3D lambda { |str| puts str }

ANOTHER_LAMBDA =3D -> { puts 3 }

def return_the_lambda
  lambda { |str| puts str }
end



=E2=9D=AF irb -I. -rlambda
irb(main):001:0> $new_db
=3D> #<Proc:0x00007f9694878ef8@/Users/rsahae/lambda.rb:3 (lambda)>
irb(main):002:0> SOME_LAMBDA
=3D> #<Proc:0x00007f9694878e58@/Users/rsahae/lambda.rb:11 (lambda)>
irb(main):003:0> ANOTHER_LAMBDA
=3D> #<Proc:0x00007f9694878e30@/Users/rsahae/lambda.rb:13 (lambda)>
irb(main):004:0> return_the_lambda
=3D> #<Proc:0x00007f96938a9630@/Users/rsahae/lambda.rb:16 (lambda)>
irb(main):005:0>



-Raj


On Fri, Dec 6, 2019 at 9:21 PM Heng Li <[email protected]> wrote:

> Thanks, Raj. I tried your suggestion. it return the the same error. I'm i=
n
> the directory where lambda.rb file is located. so it look like not the
> issue of the file path
>
> -Heng
>
> On Fri, Dec 6, 2019 at 8:48 PM Raj Sahae <[email protected]> wrote:
>
>> the problem is your load path.  try
>>
>> irb -I $PWD -r lamba
>>
>> - Raj
>>
>> On Fri, Dec 6, 2019, 7:18 PM Heng Li <[email protected]> wrote:
>>
>>> Hi all,
>>> This is my lambda.rb file.  first, I run  irb -r ./lambda_counter.rb
>>> load file  from terminal. then I try call db =3D new_db.call . I got
>>> "NameError: undefined local variable or method `new_db' for main:Object=
". i
>>> aslo try load file inside irb, use require 'lambda.rb', it's not workin=
g as
>>> well.
>>> My os is mac os, ruby is 2.3.3
>>> new_db =3D lambda do
>>> db =3D{}
>>> insert =3D lambda do |artist, album|
>>> db[artist] =3Dalbum
>>> end
>>> {insert: insert}
>>> end
>>>
>>> Unsubscribe: <mailto:[email protected]
>>> ?subject=3Dunsubscribe>
>>> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
>>>
>>
>> Unsubscribe: <mailto:[email protected]?subject=3Dunsubscri=
be>
>> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
>>
>
> Unsubscribe: <mailto:[email protected]?subject=3Dunsubscrib=
e>
> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
>

--0000000000001e9dbf05991a14b0
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">You are right, it&#39;s not the load path. I&#39;m not sur=
e exactly why but the variables holding the procs are maybe going out of sc=
ope? If you make them globals, constants, or methods, then it works.<div><b=
r></div><div><font face=3D"monospace">=E2=9D=AF cat lambda.rb<br>#!/usr/bin=
/env ruby<br><br>$new_db =3D lambda do<br>=C2=A0 db =3D{}<br>=C2=A0 insert =
=3D lambda do |artist, album|<br>=C2=A0 =C2=A0 db[artist] =3Dalbum<br>=C2=
=A0 end<br>=C2=A0 {insert: insert}<br>end<br><br>SOME_LAMBDA =3D lambda { |=
str| puts str }<br><br>ANOTHER_LAMBDA =3D -&gt; { puts 3 }<br><br>def retur=
n_the_lambda<br>=C2=A0 lambda { |str| puts str }<br>end<br><br><br></font><=
/div><div><font face=3D"monospace"><br></font></div><div><font face=3D"mono=
space">=E2=9D=AF irb -I. -rlambda<br>irb(main):001:0&gt; $new_db<br>=3D&gt;=
 #&lt;Proc:0x00007f9694878ef8@/Users/rsahae/lambda.rb:3 (lambda)&gt;<br>irb=
(main):002:0&gt; SOME_LAMBDA<br>=3D&gt; #&lt;Proc:0x00007f9694878e58@/Users=
/rsahae/lambda.rb:11 (lambda)&gt;<br>irb(main):003:0&gt; ANOTHER_LAMBDA<br>=
=3D&gt; #&lt;Proc:0x00007f9694878e30@/Users/rsahae/lambda.rb:13 (lambda)&gt=
;<br>irb(main):004:0&gt; return_the_lambda<br>=3D&gt; #&lt;Proc:0x00007f969=
38a9630@/Users/rsahae/lambda.rb:16 (lambda)&gt;<br>irb(main):005:0&gt;</fon=
t></div><div><br></div><div><br></div><div><br clear=3D"all"><div><div dir=
=3D"ltr" class=3D"gmail_signature" data-smartmail=3D"gmail_signature"><div>=
-Raj</div></div></div><br></div></div><br><div class=3D"gmail_quote"><div d=
ir=3D"ltr" class=3D"gmail_attr">On Fri, Dec 6, 2019 at 9:21 PM Heng Li &lt;=
<a href=3D"mailto:[email protected]">[email protected]</a>&gt=
; wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px=
 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div di=
r=3D"ltr">Thanks, Raj. I tried your suggestion. it return the the same erro=
r. I&#39;m in the directory where lambda.rb file is located. so it look lik=
e not the issue of the file path=C2=A0<div><br></div><div>-Heng=C2=A0=C2=A0=
</div></div><br><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_=
attr">On Fri, Dec 6, 2019 at 8:48 PM Raj Sahae &lt;<a href=3D"mailto:rajsah=
[email protected]" target=3D"_blank">[email protected]</a>&gt; wrote:<br></div>=
<blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-=
left:1px solid rgb(204,204,204);padding-left:1ex"><div dir=3D"auto"><div>th=
e problem is your load path.=C2=A0 try<div dir=3D"auto"><br></div><div dir=
=3D"auto">irb -I $PWD -r lamba<br><br><div dir=3D"auto">- Raj</div></div><b=
r><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Fri, =
Dec 6, 2019, 7:18 PM Heng Li &lt;<a href=3D"mailto:[email protected]=
" target=3D"_blank">[email protected]</a>&gt; wrote:<br></div><block=
quote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1=
px solid rgb(204,204,204);padding-left:1ex"><div dir=3D"ltr">Hi all,<div>Th=
is is my lambda.rb file.=C2=A0 first, I run=C2=A0 irb -r ./lambda_counter.r=
b load file=C2=A0 from terminal. then I try call db =3D new_db.call . I got=
 &quot;NameError: undefined local variable or method `new_db&#39; for main:=
Object&quot;. i aslo try load file inside irb, use require=C2=A0&#39;lambda=
.rb&#39;, it&#39;s not working as well.</div><div>My os is mac os, ruby is =
2.3.3=C2=A0</div><div><div style=3D"color:rgb(212,212,212);background-color=
:rgb(30,30,30);font-family:Menlo,Monaco,&quot;Courier New&quot;,monospace;f=
ont-size:12px;line-height:18px;white-space:pre-wrap"><div><span style=3D"co=
lor:rgb(156,220,254)">new_db</span> =3D <span style=3D"color:rgb(220,220,17=
0)">lambda</span> <span style=3D"color:rgb(197,134,192)">do</span> </div><d=
iv>  <span style=3D"color:rgb(156,220,254)">db</span> =3D{}</div><div>  <sp=
an style=3D"color:rgb(156,220,254)">insert</span> =3D <span style=3D"color:=
rgb(220,220,170)">lambda</span> <span style=3D"color:rgb(197,134,192)">do</=
span> |<span style=3D"color:rgb(156,220,254)">artist</span>, <span style=3D=
"color:rgb(156,220,254)">album</span>|</div><div>    db[<span style=3D"colo=
r:rgb(156,220,254)">artist</span>] =3Dalbum</div><div>  <span style=3D"colo=
r:rgb(197,134,192)">end</span></div><div>  {<span style=3D"color:rgb(86,156=
,214)">insert:</span> <span style=3D"color:rgb(156,220,254)">insert</span>}=
</div><div><span style=3D"color:rgb(197,134,192)">end</span></div></div></d=
iv></div>
<br>
Unsubscribe: &lt;mailto:<a href=3D"mailto:[email protected]" =
rel=3D"noreferrer" target=3D"_blank">[email protected]</a>?su=
bject=3Dunsubscribe&gt;<br>
&lt;<a href=3D"http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk=
" rel=3D"noreferrer noreferrer" target=3D"_blank">http://lists.ruby-lang.or=
g/cgi-bin/mailman/options/ruby-talk</a>&gt;<br>
</blockquote></div></div></div>
<br>
Unsubscribe: &lt;mailto:<a href=3D"mailto:[email protected]" =
target=3D"_blank">[email protected]</a>?subject=3Dunsubscribe=
&gt;<br>
&lt;<a href=3D"http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk=
" rel=3D"noreferrer" target=3D"_blank">http://lists.ruby-lang.org/cgi-bin/m=
ailman/options/ruby-talk</a>&gt;<br>
</blockquote></div>
<br>
Unsubscribe: &lt;mailto:<a href=3D"mailto:[email protected]" =
target=3D"_blank">[email protected]</a>?subject=3Dunsubscribe=
&gt;<br>
&lt;<a href=3D"http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk=
" rel=3D"noreferrer" target=3D"_blank">http://lists.ruby-lang.org/cgi-bin/m=
ailman/options/ruby-talk</a>&gt;<br>
</blockquote></div>

--0000000000001e9dbf05991a14b0--

--===============0043678004==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline


Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

--===============0043678004==--