[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to create a block consisting of multiple lines.

Raj Singh

8/24/2008 1:04:00 AM

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.
--
Posted via http://www.ruby-....

2 Answers

Victor H. Goff III

8/24/2008 1:14:00 AM

0

[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'
}


On Sat, Aug 23, 2008 at 8:03 PM, Raj Singh <neeraj.jsr@gmail.com> wrote:

> 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.
> --
> Posted via http://www.ruby-....
>
>


--
Warmest Regards,

Victor H. Goff III
Voice (218) 206-2470

Joel VanderWerf

8/24/2008 1:18:00 AM

0

Raj Singh wrote:
> 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.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407