# Ruby 1.8.7 Lambas: syntax and scoping issue (are these bugs?

**URL:** <https://www.ruby-forum.com/t/ruby-1-8-7-lambas-syntax-and-scoping-issue-are-these-bugs/154970>\
**Category:** Ruby\
**Created:** [December 5, 2008, 6:00pm UTC](https://www.ruby-forum.com/t/ruby-1-8-7-lambas-syntax-and-scoping-issue-are-these-bugs/154970 "2008-12-05T18:00:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![charlton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c5a1d2/32.png) [@charlton](https://www.ruby-forum.com/u/charlton)\
**Post date:** [December 5, 2008, 6:00pm UTC](https://www.ruby-forum.com/t/ruby-1-8-7-lambas-syntax-and-scoping-issue-are-these-bugs/154970/1 "2008-12-05T18:00:17Z")

</div>

I’m seeing two odd behaviours with using lambdas:

1. It seems as though having a lambda as a function argument with a  
block causes a syntax error. It can be mitigated by adding a semicolon  
or by using parentheses

def foo(rest)  
puts rest.inspect  
yield  
end

# fails syntax error

foo :parm1=\>1, :parm2=\>lambda { puts “lambda” } do  
puts “In bar block”  
end

# fails syntax error

foo :parm1=\>lambda { puts “lambda” }, :parm2=\>1 do  
puts “In bar block”  
end

# fails syntax error

foo {:parm1=\>1, :parm2=\>lambda { puts “lambda” }} do  
puts “In bar block”  
end

# succeeds???

foo :parm1=\>1, :parm2=\>lambda { puts “lambda”; } do  
puts “In bar block”  
end

# succeeds???

foo (:parm1=\>1, :parm2=\>lambda { puts “lambda” }) do  
puts “In bar block”  
end

1. I’m not understanding the scoping rules for lambda with  
instance\_evals inside a class:

# If these statements are added at the beginning the

# the lambda will evaluate them first

# a = 4 # stmt 1

# b = 5 # stmt 2

class Bar  
attr\_accessor :a, :b  
def initialize  
@a = 1  
@b = 2  
end  
def foo(&block)  
instance\_eval(&block)  
end  
end

m = lambda{puts a + b}  
Bar.new.foo(&m)

Without stmt1 and stmt2 above, the code correctly outputs 3. But if  
stmt1 and stmt2 are incommented, the output is 9 which seems to be the  
global scope rather than the scope of the instance of Bar expected  
with instance\_eval.

Am I missing something?

Thanks,  
Charlton

---

<div class="post-metadata">

**Author:** ![charlton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c5a1d2/32.png) [@charlton](https://www.ruby-forum.com/u/charlton)\
**Post date:** [December 6, 2008, 9:57pm UTC](https://www.ruby-forum.com/t/ruby-1-8-7-lambas-syntax-and-scoping-issue-are-these-bugs/154970/2 "2008-12-06T21:57:57Z")

</div>

Charlton Wang wrote:

> # fails syntax error
> 
> foo :parm1=\>1, :parm2=\>lambda { puts “lambda” } do  
> puts “In bar block”  
> end

The syntax is wrong whether or not you are passing  
a Proc object as an argument.

def foo(rest)  
puts rest.inspect  
yield  
end  
==\>nil  
foo ‘hello’ { puts ‘in block’ }  
SyntaxError: compile error  
(irb):5: syntax error  
foo ‘hello’ { puts ‘in block’ }  
^  
(irb):5: syntax error  
from (irb):5  
from :0  
foo( ‘hello’ ){ puts ‘in block’ }  
“hello”  
in block  
==\>nil  
foo( proc{p “I’m neither sheep nor lambda.”} ){ puts ‘in block’ }  
#Proc:0x02b14a5c@:6(irb)  
in block  
==\>nil

---

<div class="post-metadata">

**Author:** ![charlton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c5a1d2/32.png) [@charlton](https://www.ruby-forum.com/u/charlton)\
**Post date:** [December 5, 2008, 6:27pm UTC](https://www.ruby-forum.com/t/ruby-1-8-7-lambas-syntax-and-scoping-issue-are-these-bugs/154970/3 "2008-12-05T18:27:56Z")

</div>

Hi –

On Sat, 6 Dec 2008, Charlton Wang wrote:

> I’m seeing two odd behaviours with using lambdas:

I’m skipping to #2 if that’s OK.

> ```
> @a = 1
> 
> ```
> 
> Without stmt1 and stmt2 above, the code correctly outputs 3. But if  
> stmt1 and stmt2 are incommented, the output is 9 which seems to be the  
> global scope rather than the scope of the instance of Bar expected  
> with instance\_eval.
> 
> Am I missing something?

When Ruby sees a and b, it favors the interpretation that they are  
local variables. If you want to ensure that they’re interpreted as  
method calls, you can do:

puts a() + b()

David

---

<div class="post-metadata">

**Author:** ![charlton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c5a1d2/32.png) [@charlton](https://www.ruby-forum.com/u/charlton)\
**Post date:** [December 7, 2008, 3:20am UTC](https://www.ruby-forum.com/t/ruby-1-8-7-lambas-syntax-and-scoping-issue-are-these-bugs/154970/4 "2008-12-07T03:20:38Z")

</div>

Sorry, you lost me with your example.

My block is being bound with do/end rather than {}. In your examples,  
you’re using {} which has tighter binding and the syntax error is  
expected.

foo ‘hello’ do puts ‘in block’; end

works just find but this isn’t the same as what I’m asking. I’m binding  
the lambda function as a hash value.

Or…maybe I’m missing something obvious.

Charlton

William J. wrote:

> Charlton Wang wrote:
> 
> > # fails syntax error
> > 
> > foo :parm1=\>1, :parm2=\>lambda { puts “lambda” } do  
> > puts “In bar block”  
> > end
> 
> The syntax is wrong whether or not you are passing  
> a Proc object as an argument.
> 
> def foo(rest)  
> puts rest.inspect  
> yield  
> end  
> ==\>nil  
> foo ‘hello’ { puts ‘in block’ }  
> SyntaxError: compile error  
> (irb):5: syntax error  
> foo ‘hello’ { puts ‘in block’ }  
> ^  
> (irb):5: syntax error  
> from (irb):5  
> from :0  
> foo( ‘hello’ ){ puts ‘in block’ }  
> “hello”  
> in block  
> ==\>nil  
> foo( proc{p “I’m neither sheep nor lambda.”} ){ puts ‘in block’ }  
> #Proc:0x02b14a5c@:6(irb)  
> in block  
> ==\>nil

---

<div class="post-metadata">

**Author:** ![charlton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c5a1d2/32.png) [@charlton](https://www.ruby-forum.com/u/charlton)\
**Post date:** [December 7, 2008, 4:18pm UTC](https://www.ruby-forum.com/t/ruby-1-8-7-lambas-syntax-and-scoping-issue-are-these-bugs/154970/5 "2008-12-07T16:18:00Z")

</div>

The Higgs bozo wrote:

> Charlton Wang wrote:
> 
> > # fails syntax error
> > 
> > foo :parm1=\>1, :parm2=\>lambda { puts “lambda” } do  
> > puts “In bar block”  
> > end
> 
> Curiously, that works in 1.9.
> 
> I also noticed this succeeds in 1.8 (and 1.9):
> 
> foo :parm1=\>1, :parm2=\>lambda { puts(“lambda”) } do  
> puts “In bar block”  
> end

That’s really interesting. So the parentheses as arguments to the puts  
make it work. How odd!

---

<div class="post-metadata">

**Author:** ![charlton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c5a1d2/32.png) [@charlton](https://www.ruby-forum.com/u/charlton)\
**Post date:** [December 7, 2008, 8:48am UTC](https://www.ruby-forum.com/t/ruby-1-8-7-lambas-syntax-and-scoping-issue-are-these-bugs/154970/6 "2008-12-07T08:48:17Z")

</div>

Charlton Wang wrote:

> # fails syntax error
> 
> foo :parm1=\>1, :parm2=\>lambda { puts “lambda” } do  
> puts “In bar block”  
> end

Curiously, that works in 1.9.

I also noticed this succeeds in 1.8 (and 1.9):

foo :parm1=\>1, :parm2=\>lambda { puts(“lambda”) } do  
puts “In bar block”  
end
