passing blocks and such 
Author Message
 passing blocks and such

i was just playing around with some ideas and came across something i could
not do although it seems to me that it wouldn't be unreasonable to want to do
so:

        def a_block_like_method(i)
                puts i
        end

        arr = [1, 2, 3]
        arr.each do a_block_like_method(i)

i also tried

        def pass_a_block
                { |i| puts i }
        end

        arr = [1, 2, 3]
        arr.each do pass_a_block

and i tried a few other variations with no success. is there a way to pass a
block or use a method as a block?

--
tom sawyer, aka transami



Sat, 06 Aug 2005 05:59:48 GMT  
 passing blocks and such

Quote:

> i was just playing around with some ideas and came across something i could
> not do although it seems to me that it wouldn't be unreasonable to want to do
> so:

>    def a_block_like_method(i)
>            puts i
>    end

>    arr = [1, 2, 3]
>    arr.each do a_block_like_method(i)

Try this:

         arr.each &(method :a_block_like_method)



Sat, 06 Aug 2005 06:04:11 GMT  
 passing blocks and such

Quote:

> i also tried

>    def pass_a_block
>            { |i| puts i }
>    end

>    arr = [1, 2, 3]
>    arr.each do pass_a_block

This looks very similar:

        def pass_a_block
                proc { |i| puts i }
        end

        arr = [1, 2, 3]
        arr.each &pass_a_block

But that's probably not what you want. It's not the method, it's a Proc
object (which happens to be the returned object from the method) that's
being iterated. You can use a local variable instead of a method:

        pass_a_block = proc { |i| puts i }

        arr = [1, 2, 3]
        arr.each &pass_a_block

B.



Sat, 06 Aug 2005 06:38:25 GMT  
 passing blocks and such

These are the alternatives as far as I can see (plus combinations):

irb(main):001:0> def a_block_like_method(i)
irb(main):002:1> puts i
irb(main):003:1> end
nil
irb(main):004:0> arr = [1, 2, 3]
[1, 2, 3]
irb(main):005:0> arr.each do |i| a_block_like_method(i) end
1
2
3
[1, 2, 3]
irb(main):006:0> block = Proc.new {|i| puts i}
#<Proc:0x2ab73f0>
irb(main):007:0> arr.each do |i| block.call(i) end
1
2
3
[1, 2, 3]
irb(main):008:0> arr.each &block
1
2
3
[1, 2, 3]
irb(main):009:0>

Why isn't this sufficient?

Regards

    robert



Quote:

> i was just playing around with some ideas and came across something i
could
> not do although it seems to me that it wouldn't be unreasonable to want
to do
> so:

> def a_block_like_method(i)
> puts i
> end

> arr = [1, 2, 3]
> arr.each do a_block_like_method(i)

> i also tried

> def pass_a_block
> { |i| puts i }
> end

> arr = [1, 2, 3]
> arr.each do pass_a_block

> and i tried a few other variations with no success. is there a way to
pass a
> block or use a method as a block?

> --
> tom sawyer, aka transami




Mon, 08 Aug 2005 17:17:27 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Can We Pass Block from Function to Function?

2. passing blocks

3. Recursively passing blocks...

4. passing blocks to Proc objects

5. Block passing: obj.new(){block}

6. std blocks vs blocks+cache ( was: block behavior)

7. passing variables into blocks

8. Reading the code passed inside a block?

9. passing on a block

10. passing a block from C to Ruby

11. Passing multiple blocks to a method

12. Passing on a block to a called method

 

 
Powered by phpBB® Forum Software