[OpenStruct] unexpected behaviour, bug?

B

benny

dear list,

just noticed an unexspected behaviour of the OpenStruct class:


require 'ostruct'
t = OpenStruct.new()
t.meth1 = []
t.send('meth1') << "hi"
puts t.meth1.inspect

this works, but

t.send('meth2') = [] # => test.rb:17: syntax error t.send('meth2') = []
puts t.meth2.inspect

is it a bug or am I missing something?

greetings,

benny
 
T

trans. (T. Onoma)

On Monday 17 January 2005 07:16 am, benny wrote:
| dear list,
|
| just noticed an unexspected behaviour of the OpenStruct class:
|
|
| require 'ostruct'
| t = OpenStruct.new()
| t.meth1 = []
| t.send('meth1') << "hi"
| puts t.meth1.inspect
|
| this works, but
|
| t.send('meth2') = [] # => test.rb:17: syntax error t.send('meth2') = []
| puts t.meth2.inspect

aassignment itself isn't a method in Ruby, but assignment methods are (i.e.
setter methods), so try:

t.send('meth2=', [])

T
 
B

benny

trans. (T. Onoma) said:
On Monday 17 January 2005 07:16 am, benny wrote:
| dear list,
|
| just noticed an unexspected behaviour of the OpenStruct class:
|
|
| require 'ostruct'
| t = OpenStruct.new()
| t.meth1 = []
| t.send('meth1') << "hi"
| puts t.meth1.inspect
|
| this works, but
|
| t.send('meth2') = [] # => test.rb:17: syntax error t.send('meth2') =
| [] puts t.meth2.inspect

aassignment itself isn't a method in Ruby, but assignment methods are
(i.e. setter methods), so try:

t.send('meth2=', [])

T
second lesson :)

benny
 

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

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top