Struct (not) accepting attributes

K

Kero

Sum = Struct.new:)bla, :"sum(value)")
sum = Sum.new(2, 5)
=> # said:
sum.send:)"sum(value)")
NoMethodError: undefined method `sum(value)' for # said:
class X
attr_accessor :"sum(value)"
end
NameError: invalid attribute name `sum(value)'
class Sum
define_method:)"sum(value)") { "summum!" }
end
=> # said:
sum.send:)"sum(value)")
=> "summum!"

Not sure this is an error in ruby (1.8.5) or a misunderstanding from me (about Struct).
And yes, I know how to get/set the value anyway:
sum[:"sum(value)"] => 5
sum["sum(value)"] = 42
=> 42

Bye,
Kero.
 
R

Robert Klemme

NameError: invalid attribute name `sum(value)'

=> "summum!"

Not sure this is an error in ruby (1.8.5) or a misunderstanding from me (about Struct).

There is another option: a misunderstanding on your side which attribute
names can be used in this situation / generally. As you see your sample
class X does not work either.

Why don't you just use "sum_value" as attribute name?

Kind regards

robert
 
J

Jan Svitok

There is another option: a misunderstanding on your side which attribute
names can be used in this situation / generally. As you see your sample
class X does not work either.

Why don't you just use "sum_value" as attribute name?

Kind regards

robert

sum(value) is not a valid method name. method names must match (I
believe) /[a-z_][a-zA-z0-9_]+[?!]?/

if you want to have a struct with additional method do this:

class Sum < Struct.new:)blah)
def sum(value)
...
end
end
 
K

Kero

Sum = Struct.new:)bla, :"sum(value)")
There is another option: a misunderstanding on your side which attribute
names can be used in this situation / generally. As you see your sample
class X does not work either.

class X throws an Exception, Struct.new does not.
1) Because Struct does not use attributes
2) because it only lost one way (not both) to access the value for my obscure
symbol name.

But the define_method *does* work :)
I can call it w/ #send !
So I can not do

irb> class Sum
irb> define_method:)"sum(value)") { instance_variable_get("@sum(value)") }
irb> end
irb> sum.send:)"sum(value)")
NameError: `@sum(value)' is not allowed as an instance variable name

but I can do

irb> class Sum
irb> define_method:)"sum(value)") { self["sum(value)"] }
irb> end
=> #<Proc:0xa7c6535c@(irb):21>
irb> sum.send:)"sum(value)")
=> 5

so <pedantic> why doesn't Struct? </pedantic> :p

also, `ruby -dw` does not give a warning for Struct.new:)bla, :"sum(value)").
Why don't you just use "sum_value" as attribute name?

Because "sum(value)" is generated by an SQL query.

I can't do record_from_db.sum(value), and obviously resolving to #send is
both too verbose and something I do not desire to explain to anyone as a
proper API. So record_from_db["sum(value)"] suits me fine.

This post is food for thought, if anything :)

Bye,
Kero.
 

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,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top