[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

OpenStruct and #send

Hal E. Fulton

10/27/2004 8:17:00 AM

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>



7 Answers

Robert Klemme

10/27/2004 8:45:00 AM

0


"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? :)
>
> 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

Yukihiro Matsumoto

10/27/2004 8:48:00 AM

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? :)
|
|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 E. Fulton

10/27/2004 9:49:00 AM

0

Robert Klemme wrote:

> This looks like an IRB anomaly:

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

Hal



Robert Klemme

10/27/2004 10:04:00 AM

0


"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

Hal E. Fulton

10/27/2004 10:13:00 AM

0

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



Robert Klemme

10/27/2004 11:19:00 AM

0


"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

Florian Gross

10/27/2004 1:35:00 PM

0

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? :)
> |
> |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.