How to create a block consisting of multiple lines.

R

Raj Singh

This works.

class Person
def hi(inputproc)
inputproc.call
end
end

Person.new.hi lambda { puts 'wow1'; puts 'wow2'}

However when I try to split the code inside block in multiple line the
code does NOT work anymore.

This does NOT work.

class Person
def hi(inputproc)
inputproc.call
end
end

Person.new.hi lambda do
puts 'wow1'
puts 'wow2'
end


How do I create a proc consisting of multiple lines of code.
 
V

Victor H. Goff III

[Note: parts of this message were removed to make it a legal post.]

class Person
def hi(inputproc)
inputproc.call
end
end

Person.new.hi lambda {
puts 'wow1'
puts 'wow2'
}
 
J

Joel VanderWerf

Raj said:
This works.

class Person
def hi(inputproc)
inputproc.call
end
end

Person.new.hi lambda { puts 'wow1'; puts 'wow2'}

However when I try to split the code inside block in multiple line the
code does NOT work anymore.

This does NOT work.

class Person
def hi(inputproc)
inputproc.call
end
end

Person.new.hi lambda do
puts 'wow1'
puts 'wow2'
end


How do I create a proc consisting of multiple lines of code.

do..end and {..} have different precedence. Your multiline example
should work of you use {..} instead.

Or you can force precedence with (..).

What's going on with:
Person.new.hi lambda do
puts 'wow1'
puts 'wow2'
end

is that lambda is interpreted as the first param to #hi, and the do..end
block is passed as a block to #hi (rather than to #lambda).

HTH.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top