Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Struct (not) accepting attributes

Reply
Thread Tools

Struct (not) accepting attributes

 
 
Kero
Guest
Posts: n/a
 
      10-18-2006
> Sum = Struct.new(:bla, :"sum(value)")
> sum = Sum.new(2, 5)

=> #<struct Sum bla=2, :"sum(value)"=5>
> sum.send(:"sum(value)")

NoMethodError: undefined method `sum(value)' for #<struct Sum bla=2, :"sum(value)"=5>
> class X
> attr_accessor :"sum(value)"
> end

NameError: invalid attribute name `sum(value)'
> class Sum
> define_method(:"sum(value)") { "summum!" }
> end

=> #<Proc:0xa7c5f218@(irb):181>
> 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.
 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      10-18-2006
On 18.10.2006 14:33, Kero wrote:
>> Sum = Struct.new(:bla, :"sum(value)")
>> sum = Sum.new(2, 5)

> => #<struct Sum bla=2, :"sum(value)"=5>
>> sum.send(:"sum(value)")

> NoMethodError: undefined method `sum(value)' for #<struct Sum bla=2, :"sum(value)"=5>
>> class X
>> attr_accessor :"sum(value)"
>> end

> NameError: invalid attribute name `sum(value)'
>> class Sum
>> define_method(:"sum(value)") { "summum!" }
>> end

> => #<Proc:0xa7c5f218@(irb):181>
>> sum.send(:"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
 
Reply With Quote
 
 
 
 
Jan Svitok
Guest
Posts: n/a
 
      10-18-2006
On 10/18/06, Robert Klemme <(E-Mail Removed)> wrote:
> On 18.10.2006 14:33, Kero wrote:
> >> Sum = Struct.new(:bla, :"sum(value)")
> >> sum = Sum.new(2, 5)

> > => #<struct Sum bla=2, :"sum(value)"=5>
> >> sum.send(:"sum(value)")

> > NoMethodError: undefined method `sum(value)' for #<struct Sum bla=2, :"sum(value)"=5>
> >> class X
> >> attr_accessor :"sum(value)"
> >> end

> > NameError: invalid attribute name `sum(value)'
> >> class Sum
> >> define_method(:"sum(value)") { "summum!" }
> >> end

> > => #<Proc:0xa7c5f218@(irb):181>
> >> sum.send(:"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


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

 
Reply With Quote
 
Kero
Guest
Posts: n/a
 
      10-19-2006
>>> Sum = Struct.new(:bla, :"sum(value)")
>>> sum = Sum.new(2, 5)

>> => #<struct Sum bla=2, :"sum(value)"=5>
>>> sum.send(:"sum(value)")

>> NoMethodError: undefined method `sum(value)' for #<struct Sum bla=2, :"sum(value)"=5>
>>> class X
>>> attr_accessor :"sum(value)"
>>> end

>> NameError: invalid attribute name `sum(value)'
>>> class Sum
>>> define_method(:"sum(value)") { "summum!" }
>>> end

>> => #<Proc:0xa7c5f218@(irb):181>
>>> sum.send(:"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.


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>

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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
class attributes & data attributes james_027 Python 2 06-20-2007 03:12 PM
WebControl.Attributes.Add and custom attributes P4trykx ASP .Net 2 01-31-2007 04:33 PM
Parse reserved attributes as normal attributes Max XML 1 09-22-2006 12:04 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments