but that doesn't work. I could maybe force it to work by manually
creating setter methods, but that's not an elegant solution either, I'd
rather have the occassional use of instance_variable_set than craft
setters.
On Fri, Oct 29, 2010 at 10:31 PM, Jeremy Bopp <jeremy@bopp.net> wrote:
On 10/29/2010 09:14 PM, Greg Willits wrote:
> If I need to dynamically reference instance vars, is this the only way
> to do it (var set example)?
>
> my_object.send(:instance_variable_set, "@#{iname}", ivalue)
>
> I expected something more elegant, but this is the only way I can get it
> to work. No biggie, just curious.
>
> More complete example below.
>
> -- gw
>
>
> class Shape
> attr_accessor :size, :fill_color, :line_color, :line_width
> def initialize
> @size = ""
> @fill_color = ""
> @line_color = ""
> @line_width = ""
> end
> end
>
> my_shape = Shape.new
>
> shape_details = {
> :size => 'small',
> :fill_color => 'red',
> :line_color => 'black',
> :line_width => '2'}
>
> shape_details.each do |iname, ivalue|
> my_shape.send(:instance_variable_set, "@#{iname}", ivalue)
> end
How about backing the accessors with a hash that also has an accessor?
That way you could merge in a hash of settings or set/get them neatly by
name:
but that doesn't work. I could maybe force it to work by manually
creating setter methods, but that's not an elegant solution either, I'd
rather have the occassional use of instance_variable_set than craft
setters.
In another language I used, I could simply do the equivalent of
myobject.iname = ivalue
This breaks the encapsulation that objects are supposed to provide for their
hidden state. I have to say I *like* the fact that
instance_variable_get/instance_variable_set are long and unwieldy. By using
them you hopefully remind yourself that you're doing something naughty by
touching an object's private parts.
I was hoping in Ruby that at least this was possible:
but that doesn't work. I could maybe force it to work by manually
creating setter methods, but that's not an elegant solution either, I'd
rather have the occassional use of instance_variable_set than craft
setters.
It's Ruby, a lot is possible! You could add some method_missing magic so:
myobj.iv_foobar = 42
thunks to:
instance_variable_set(:@foobar, 42)
I would strongly recommend against that sort of thing though.
···
On Sat, Oct 30, 2010 at 1:27 PM, Greg Willits <lists@gregwillits.ws> wrote:
You're confusing methods and variables. They're not the same, but you're on the right track with the code above, but you'd be calling the getter, not the setter. Check it:
class X
attr_accessor :x # creates x and x= methods
end
o = X.new
o.send("x=", 42)
p x
···
On Oct 30, 2010, at 12:27 , Greg Willits wrote:
I was hoping in Ruby that at least this was possible:
You're confusing methods and variables. They're not the same, but you're
on the right track with the code above, but you'd be calling the getter,
not the setter. Check it:
class X
attr_accessor :x # creates x and x= methods
end
o = X.new
o.send("x=", 42)
p x
I wasn't confusing them, I was hoping Ruby's send would be flexible
enough to work with both of them (in conjunction with the accessors
being defined). What I didn't think of in this context was Ruby's x=
being considered the setter method and not just x, which I knew, and
should have recognized -- so yep, that was the ticket. You win!
Sorry, no prizes
You're confusing methods and variables. They're not the same, but you're
on the right track with the code above, but you'd be calling the getter,
not the setter. Check it:
class X
attr_accessor :x # creates x and x= methods
end
o = X.new
o.send("x=", 42)
p x
I wasn't confusing them, I was hoping Ruby's send would be flexible
enough to work with both of them (in conjunction with the accessors
being defined). What I didn't think of in this context was Ruby's x=
being considered the setter method and not just x, which I knew, and
should have recognized -- so yep, that was the ticket. You win!
Sorry, no prizes