The following will work with arbitrary objects …
obj.instance_eval(“lambda { |v| @a = v }”).call(value)
Well, you can do it simpler, even if you want to use
instance_eval. No need for a lambda:
irb(main):001:0> class Foo
irb(main):002:1> def test(obj)
irb(main):003:2> instance_eval(“@x=obj”)
irb(main):004:2> end
irb(main):005:1> end
I stand corrected. For some reason I thought that the value of ‘obj’
wouldn’t be available inside of an instance_eval.
At this point you must be thinking: “Why don’t I just use
instance_variable_set?”
I’d love to see the answer to this question you’d suggest. :-))
The question was meant to be rhetorical, although with the simplification
the choice is less clear.
···
–
– Jim Weirich / Compuware
– FWP Capture Services
– Phone: 859-386-8855
Robert
(Robert)
2
“Weirich, James” James.Weirich@FMR.COM schrieb im Newsbeitrag
news:1C8557C418C561429998C1F8FBB283A728BA95@MSGDALCLB2WIN.DMN1.FMR.COM…
The following will work with arbitrary objects …
obj.instance_eval(“lambda { |v| @a = v }”).call(value)
Well, you can do it simpler, even if you want to use
instance_eval. No need for a lambda:
irb(main):001:0> class Foo
irb(main):002:1> def test(obj)
irb(main):003:2> instance_eval(“@x=obj”)
irb(main):004:2> end
irb(main):005:1> end
I stand corrected. For some reason I thought that the value of ‘obj’
wouldn’t be available inside of an instance_eval.
As far as I can see instance_eval just changes the value of ‘self’.
At this point you must be thinking: “Why don’t I just use
instance_variable_set?”
I’d love to see the answer to this question you’d suggest. :-))
The question was meant to be rhetorical,
That’s what I assumed. I thought you might come up with a funny answer…
![:slight_smile: :slight_smile:](https://emoji.discourse-cdn.com/twitter/slight_smile.png?v=12)
although with the simplification
the choice is less clear.
Yes, that’s true. And, as we have seen, instance_eval is more backward
compatible.
Kind regards
robert
ts1
(ts)
3
As far as I can see instance_eval just changes the value of 'self'.
it change also the class
svg% ruby -e 'b = ; b.instance_eval { def a() puts "array"; end}; b.a'
array
svg%
Guy Decoux
Robert
(Robert)
4
“ts” decoux@moulon.inra.fr schrieb im Newsbeitrag
news:200401191613.i0JGDRQ14600@moulon.inra.fr…
As far as I can see instance_eval just changes the value of ‘self’.
it change also the class
svg% ruby -e ‘b = ; b.instance_eval { def a() puts “array”; end}; b.a’
array
svg%
Well, but that is just a consequence of self pointing to another instance.
You could have achieved the same by doing:
17:50:29 [w]: ruby -e ‘b = ; b.instance_eval { def self.a() puts
“array”; end}; b.a’
array
To verify it’s really an instance method:
17:50:23 [w]: ruby -e ‘b = ; b.instance_eval { def a() puts “array”;
end}; b.a; .a’
array
-e:1: undefined method `a’ for :Array (NoMethodError)
Am I overlooking something?
Kind regards
robert
ts1
(ts)
5
17:50:29 [w]: ruby -e 'b = ; b.instance_eval { def self.a() puts
"array"; end}; b.a'
array
def a() end is not the same than def self.a() end
Am I overlooking something?
nothing important
Guy Decoux
it change also the class
svg% ruby -e ‘b = ; b.instance_eval { def a() puts “array”; end}; b.a’
array
svg%
Well, but that is just a consequence of self pointing to another instance.
You could have achieved the same by doing:
17:50:29 [w]: ruby -e ‘b = ; b.instance_eval { def self.a() puts
“array”; end}; b.a’
array
[…]
Am I overlooking something?
Guy is emphasizing the fact that this modifies the singleton class.
···
On Tue, Jan 20, 2004 at 01:55:00AM +0900, Robert Klemme wrote:
–
_ _
__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_
_ \ / ` | ’ \
) | (| | |__ \ | | | | | (| | | | |
.__/ _,|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com
Linux poses a real challenge for those with a taste for late-night
hacking (and/or conversations with God).
– Matt Welsh