[Q] 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)
@set=myval
end
def set
@set
end
end

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

Thanks
Peter

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)
      @set=myval
   end
   def set
      @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

Hi –

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)
@set=myval
end
def set
@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 :slight_smile:
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)

? :slight_smile:

David

···

On Thu, 8 May 2003, Peter Schrammel wrote:


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

You can also do:

t=Test.new(“foo”) { @set = 89 }

to save some typing.

···

On Thursday, 8 May 2003 at 22:58:25 +0900, ts wrote:

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)
@set=myval
end
def set
@set
end
end

t=Test.new(“foo”) { self.set = 89 }


Jim Freeze

… and furthermore … I don’t like your trousers.

dblack@superlink.net wrote:

Are you sure you wouldn’t rather do:

t = Test.new(“foo”)
t.set(89)

? :slight_smile:
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

You can also do:
  t=Test.new("foo") { @set = 89 }
to save some typing.

Yes, but this is always the same problem : why do you want that the method
#set assign a value to the instance variable @set ?

Guy Decoux

Unfortunately you do, or else ‘@var1=89’.

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

Regards,

Brian.

···

On Fri, May 09, 2003 at 12:32:08AM +0900, Peter Schrammel wrote:

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.

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.

···

----- Original Message -----
From: “Peter Schrammel” peter.schrammel@gmx.de
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Thursday, May 08, 2003 8:32 AM
Subject: Re: [Q] Block passing: obj.new(){block}

dblack@superlink.net wrote:

Are you sure you wouldn’t rather do:

t = Test.new(“foo”)
t.set(89)

? :slight_smile:
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

Thanks a lot to everyone…