NameError: when calling lambda from irb

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 = 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 working as well.
My os is mac os, ruby is 2.3.3
new_db = lambda do
db ={}
insert = lambda do |artist, album|
db[artist] =album
end
{insert: insert}
end

the problem is your load path. try

irb -I $PWD -r lamba

- Raj

···

On Fri, Dec 6, 2019, 7:18 PM Heng Li <techitao.henry@gmail.com> 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 = 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 working as well.
My os is mac os, ruby is 2.3.3
new_db = lambda do
db ={}
insert = lambda do |artist, album|
db[artist] =album
end
{insert: insert}
end

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

Thanks, Raj. I tried your suggestion. it return the the same error. I'm in
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 <rajsahae@gmail.com> wrote:

the problem is your load path. try

irb -I $PWD -r lamba

- Raj

On Fri, Dec 6, 2019, 7:18 PM Heng Li <techitao.henry@gmail.com> 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 = 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 working as well.
My os is mac os, ruby is 2.3.3
new_db = lambda do
db ={}
insert = lambda do |artist, album|
db[artist] =album
end
{insert: insert}
end

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

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.

❯ cat lambda.rb
#!/usr/bin/env ruby

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

SOME_LAMBDA = lambda { |str| puts str }

ANOTHER_LAMBDA = -> { puts 3 }

def return_the_lambda
  lambda { |str| puts str }
end

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

-Raj

···

On Fri, Dec 6, 2019 at 9:21 PM Heng Li <techitao.henry@gmail.com> wrote:

Thanks, Raj. I tried your suggestion. it return the the same error. I'm in
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 <rajsahae@gmail.com> wrote:

the problem is your load path. try

irb -I $PWD -r lamba

- Raj

On Fri, Dec 6, 2019, 7:18 PM Heng Li <techitao.henry@gmail.com> 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 = 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 working as
well.
My os is mac os, ruby is 2.3.3
new_db = lambda do
db ={}
insert = lambda do |artist, album|
db[artist] =album
end
{insert: insert}
end

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org
?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

Variables at the top-level are local to the file. If you `require` or
`load` the file, the variable won't leak to the caller scope. The parent
code, in particular, can have its own `new_db` variable and do not worry
about loaded files messing with it. That is the point of the file scope.

You need to define that logic in some global place, using a global
variable, a constant, method in a module defined normally (not my_module =
Module.new) , etc. The choice depends on the purpose of that file.