# Struct (not) accepting attributes

**URL:** <https://www.ruby-forum.com/t/struct-not-accepting-attributes/72718>\
**Category:** Ruby\
**Created:** [October 18, 2006, 2:35pm UTC](https://www.ruby-forum.com/t/struct-not-accepting-attributes/72718 "2006-10-18T14:35:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 18, 2006, 2:35pm UTC](https://www.ruby-forum.com/t/struct-not-accepting-attributes/72718/1 "2006-10-18T14:35:50Z")

</div>

> 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@:181(irb)  
> 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.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 18, 2006, 2:41pm UTC](https://www.ruby-forum.com/t/struct-not-accepting-attributes/72718/2 "2006-10-18T14:41:52Z")

</div>

On 18.10.2006 14:33, Kero wrote:

> > define\_method(:“sum(value)”) { “summum!” }  
> > end  
> > =\> #Proc:0xa7c5f218@:181(irb)  
> > 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
```

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 18, 2006, 3:17pm UTC](https://www.ruby-forum.com/t/struct-not-accepting-attributes/72718/3 "2006-10-18T15:17:56Z")

</div>

On 10/18/06, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > class Sum  
> > > 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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 19, 2006, 3:00pm UTC](https://www.ruby-forum.com/t/struct-not-accepting-attributes/72718/4 "2006-10-19T15:00:36Z")

</div>

> > > define\_method(:“sum(value)”) { “summum!” }  
> > > end  
> > > =\> #Proc:0xa7c5f218@:181(irb)  
> > > 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@:21(irb)  
irb\> sum.send(:“sum(value)”)  
=\> 5

so why doesn’t Struct? 😛

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.
