OpenStruct and #send

H

Hal Fulton

Hmm... should this work or not?

In other words: Bug, anomaly, feature, or just user error? :)

Below, I expected/wanted x.send("foo") to give me 6.

Hal


[hal@dhcppc2 kronos]$ irb
irb(main):001:0> require 'ostruct'
=> true
irb(main):002:0> def foo
irb(main):003:1> puts "I'm a method"
irb(main):004:1> end
=> nil
irb(main):005:0> x = OpenStruct.new
=> <OpenStruct>
irb(main):006:0> x.bar = 5
=> 5
irb(main):007:0> x.foo = 6
=> 6
irb(main):008:0> x.send("bar")
=> 5
irb(main):009:0> x.send("foo")
I'm a method
=> nil
irb(main):010:0>
 
R

Robert Klemme

Hal Fulton said:
Hmm... should this work or not?

In other words: Bug, anomaly, feature, or just user error? :)

Below, I expected/wanted x.send("foo") to give me 6.

Hal


[hal@dhcppc2 kronos]$ irb
irb(main):001:0> require 'ostruct'
=> true
irb(main):002:0> def foo
irb(main):003:1> puts "I'm a method"
irb(main):004:1> end
=> nil
irb(main):005:0> x = OpenStruct.new
=> <OpenStruct>
irb(main):006:0> x.bar = 5
=> 5
irb(main):007:0> x.foo = 6
=> 6
irb(main):008:0> x.send("bar")
=> 5
irb(main):009:0> x.send("foo")
I'm a method
=> nil
irb(main):010:0>


This looks like an IRB anomaly:

10:43:19 [robert.klemme]: irbs[]
=> nil["foo"]
=> nil10:43:29 [robert.klemme]: ruby /c/temp/ruby/ostruct-foo.rb
[]
[]
10:43:35 [robert.klemme]: cat /c/temp/ruby/ostruct-foo.rb
require 'ostruct'

x = OpenStruct.new
p x.public_methods.select {|o| /^foo/ =~ o}

def foo() "buh!" end

p x.public_methods.select {|o| /^foo/ =~ o}
10:43:39 [robert.klemme]:

Kind regards

robert
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: OpenStruct and #send"

|Hmm... should this work or not?
|
|In other words: Bug, anomaly, feature, or just user error? :)
|
|Below, I expected/wanted x.send("foo") to give me 6.

Anomaly. I will fix, by defining foo and foo= at run time.

matz.
 
R

Robert Klemme

Hal Fulton said:
Not purely an irb anomaly, as it happens in a script
without irb.

This is interesting. I could not reproduce it. Under which circumstances
does it surface?

Kind regards

robert
 
H

Hal Fulton

Robert said:
This is interesting. I could not reproduce it. Under which circumstances
does it surface?

Try this script:

require 'ostruct'

def foo
puts "I'm a method"
end

x = OpenStruct.new
x.bar = 5
x.foo = 6
puts x.send("bar")
puts x.send("foo")


Rather than printing 5 and 6, for me it prints:

5
I'm a method
nil


Does it behave differently for you?

Hal
 
R

Robert Klemme

Hal Fulton said:
Try this script:

require 'ostruct'

def foo
puts "I'm a method"
end

x = OpenStruct.new
x.bar = 5
x.foo = 6
puts x.send("bar")
puts x.send("foo")


Rather than printing 5 and 6, for me it prints:

5
I'm a method
nil


Does it behave differently for you?

No. And now I see the difference: I was so fixated on checking the
instance method that I didn't notice the different behavior of #foo and
#send:)foo). Darn! Thx for your patience!

Kind regard

robert
 
F

Florian Gross

Yukihiro said:
In message "Re: OpenStruct and #send"

|Hmm... should this work or not?
|
|In other words: Bug, anomaly, feature, or just user error? :)
|
|Below, I expected/wanted x.send("foo") to give me 6.

Anomaly. I will fix, by defining foo and foo= at run time.

Thanks. This will also make it easier to find out what properties an
OpenStruct already has, I think.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top