Block passing: obj.new(){block} 
Author Message
 Block passing: obj.new(){block}

Hi,

How do I pass a block to the initialize method that calls some methods
of the new object? Here is what I want to get:

class Test
   def initialize(foo)
   ...
   end
   def set=(myval)

   end
   def set

   end
end

t=Test.new("foo"){set=89}
puts t.set
=>89
I already tried a lot but ...

Thanks
Peter



Mon, 24 Oct 2005 21:41:07 GMT  
 Block passing: obj.new(){block}

P> t=Test.new("foo"){set=89}

pigeon% cat b.rb
#!/usr/bin/ruby
class Test
   def initialize(foo, &block)
      instance_eval(&block)
   end

   def set=(myval)

   end
   def set

   end
end

t=Test.new("foo") { self.set = 89 }
puts t.set
pigeon%

pigeon% b.rb
89
pigeon%

 See `self.set=' otherwise ruby will think that you want to access the local
 variable `set'

Guy Decoux



Mon, 24 Oct 2005 21:58:25 GMT  
 Block passing: obj.new(){block}
Hi --

Quote:

> Hi,

> How do I pass a block to the initialize method that calls some methods
> of the new object? Here is what I want to get:

> class Test
>    def initialize(foo)
>    ...
>    end
>    def set=(myval)

>    end
>    def set

>    end
> end

> t=Test.new("foo"){set=89}
> puts t.set
> =>89

You're going to need to state the receiver explicitly for that call to
set (because otherwise Ruby will favor the interpretation that it's a
local variable assignment).  So, you might end up with something like:

  class Test
    attr_accessor :set       # save some typing :-)
    def initialize(foo)
      yield(self)
    end
  end

  t=Test.new("foo") {|o| o.set=89}
  puts t.set

Are you sure you wouldn't rather do:

  t = Test.new("foo")
  t.set(89)

? :-)

David

--
David Alan Black


Web:  http://pirate.shu.edu/~blackdav



Mon, 24 Oct 2005 22:03:17 GMT  
 Block passing: obj.new(){block}

Quote:


> P> t=Test.new("foo"){set=89}

> pigeon% cat b.rb
> #!/usr/bin/ruby
> class Test
>    def initialize(foo, &block)
>       instance_eval(&block)
>    end

>    def set=(myval)

>    end
>    def set

>    end
> end

> t=Test.new("foo") { self.set = 89 }

You can also do:


to save some typing.

--
Jim Freeze
----------
.. and furthermore ... I don't like your trousers.



Mon, 24 Oct 2005 23:03:39 GMT  
 Block passing: obj.new(){block}

J> You can also do:

J> to save some typing.

 Yes, but this is always the same problem : why do you want that the method

Guy Decoux



Mon, 24 Oct 2005 23:07:34 GMT  
 Block passing: obj.new(){block}
Quote:

> Are you sure you wouldn't rather do:

>   t = Test.new("foo")
>   t.set(89)

> ? :-)

I know but I'd like to envoke a lot of methods in the object on creation
and thought of something like:

t=Test.new(){var1=89,var3=89,var54="bar",onEvent("Click"){puts "You
clicked"},doSomething(1,2)}

if I had to type self.xxx everytime or do t.varxxx=xxx I'd go nuts.

Thanks for the replies so far



Mon, 24 Oct 2005 23:22:24 GMT  
 Block passing: obj.new(){block}

Quote:

> I know but I'd like to envoke a lot of methods in the object on creation
> and thought of something like:

> t=Test.new(){var1=89,var3=89,var54="bar",onEvent("Click"){puts "You
> clicked"},doSomething(1,2)}

> if I had to type self.xxx everytime or do t.varxxx=xxx I'd go nuts.


What you have written creates temporary local variables called 'var1',
'var3' etc, and then discards them at the end of the block.

Regards,

Brian.



Mon, 24 Oct 2005 23:37:06 GMT  
 Block passing: obj.new(){block}
If you want to do something "from within" an object (Test) why not to create
a method in Test to do it. Or at lease a method to set var1, var3 and var54
altogether, as they are related anyway according to your code.

In Ruby you can call object's methods from the block, that's no problem,
even for initialize(). The problem you stepped onto is exactly the one I got
into quite recently, when trying to use "method=" where Ruby interprets it
as an assignment. Look for the recent thread "Method/Assignment ambiguity"
where David Black presented an excellent explanation of inevitable problems
were Ruby treating the case not the way it does now.

Gennady.

Quote:
----- Original Message -----

Newsgroups: comp.lang.ruby

Sent: Thursday, May 08, 2003 8:32 AM
Subject: Re: [Q] Block passing: obj.new(){block}


> > Are you sure you wouldn't rather do:

> >   t = Test.new("foo")
> >   t.set(89)

> > ? :-)
> I know but I'd like to envoke a lot of methods in the object on creation
> and thought of something like:

> t=Test.new(){var1=89,var3=89,var54="bar",onEvent("Click"){puts "You
> clicked"},doSomething(1,2)}

> if I had to type self.xxx everytime or do t.varxxx=xxx I'd go nuts.

> Thanks for the replies so far



Tue, 25 Oct 2005 00:11:24 GMT  
 Block passing: obj.new(){block}
Thanks a lot to everyone...


Tue, 25 Oct 2005 00:52:45 GMT  
 
 [ 9 post ] 

 Relevant Pages 

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

2. question on return from blocks and ensure: blocks

3. ERROR "Corrupt Block/Unknown Block Freed"

4. Corrupt block/unknown block type freed bug

5. 0 BLOCK in Gforth (Was: Blocks Help etc.)

6. blocks and lambdas, or blocks as first-class entities

7. Associating block to block call?

8. blocking and non-blocking assignment

9. Strange behaviour with blocking and non-blocking assignment

10. blocking/non-blocking

11. Conversion of Altera Block RAM to Xilinx Block RAM

12. Blocking and Non-blocking assignment ?

 

 
Powered by phpBB® Forum Software