If you're just after configuring the instance variables, you might go
for:
my_obj = boxWidget.new
my_obj.instance_eval do
@x = 0
@y = 10
@width = 100
@height = 100
end
Though doing so will make you code fairly fragile - setting variables in
this way will bypass the setters, so you can't customize your setting
actions. (I was suprised that instance_eval didn't actually grant the
ability to call methods on the object without prefixing them)
Something like this maybe?
class BoxWidget
attr_accessor :x, :y, :width, :height
end
class Object
def set_all(hash)
hash.each { |k,v| send("#{k}=", v) }
end
end
my_obj = BoxWidget.new
my_obj.set_all({
:x => 10,
:y => 10,
:width => 100,
:height => 100,
})
my_obj
=> #<BoxWidget:0x2caa6d0 @y=10, @x=10, @height=100, @width=100>
···
-----Original Message-----
From: Zach Dennis [mailto:zdennis@mktec.com]
Sent: Tuesday, 24 August 2004 11:09 AM
To: ruby-talk ML
Subject: *with* block?I am looking for a structure like:
my_obj = boxWidget.new
with my_obj do
x = 0
y = 10
width = 100
height = 100
endinstead of having to say
my_obj = boxWidget.new
my_obj.x = 0
my_obj.y = 10
my_obj.width = 100
my_obj.height = 100Can you do this already? If not....anyone support an RCR for
it besides me?Zach
#####################################################################################
This email has been scanned by MailMarshal, an email content filter.
#####################################################################################