OpenStruct and #send

Hmm... should this work or not?

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

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>

Hi,

路路路

In message "Re: OpenStruct and #send" on Wed, 27 Oct 2004 17:16:50 +0900, Hal Fulton <hal9000@hypermetrics.com> writes:

Hmm... should this work or not?

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

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

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

              matz.

"Hal Fulton" <hal9000@hypermetrics.com> schrieb im Newsbeitrag
news:417F596C.5080208@hypermetrics.com...

Hmm... should this work or not?

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

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

require 'ostruct'

=> true

x = OpenStruct.new

=> <OpenStruct>

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


=> nil

def foo() "buh!" end

=> nil

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

["foo"]
=> nil

exit

10: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

Robert Klemme wrote:

This looks like an IRB anomaly:

Not purely an irb anomaly, as it happens in a script
without irb.

Hal

Yukihiro Matsumoto wrote:

路路路

In message "Re: OpenStruct and #send" > on Wed, 27 Oct 2004 17:16:50 +0900, Hal Fulton <hal9000@hypermetrics.com> writes:

>Hmm... should this work or not?
>
>In other words: Bug, anomaly, feature, or just user error? :slight_smile:
>
>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.

"Hal Fulton" <hal9000@hypermetrics.com> schrieb im Newsbeitrag
news:417F6F00.6000307@hypermetrics.com...

Robert Klemme wrote:

> This looks like an IRB anomaly:

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

Robert Klemme wrote:

"Hal Fulton" <hal9000@hypermetrics.com> schrieb im Newsbeitrag
news:417F6F00.6000307@hypermetrics.com...

Robert Klemme wrote:

This looks like an IRB anomaly:

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?

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

"Hal Fulton" <hal9000@hypermetrics.com> schrieb im Newsbeitrag
news:417F74A9.7080705@hypermetrics.com...

Robert Klemme wrote:
> "Hal Fulton" <hal9000@hypermetrics.com> schrieb im Newsbeitrag
> news:417F6F00.6000307@hypermetrics.com...
>
>>Robert Klemme wrote:
>>
>>
>>>This looks like an IRB anomaly:
>>
>>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?

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