# Extending Symbol with #to\_str effects Struct.new

**URL:** <https://www.ruby-forum.com/t/extending-symbol-with-to-str-effects-struct-new/106661>\
**Category:** Ruby\
**Created:** [June 13, 2007, 12:37am UTC](https://www.ruby-forum.com/t/extending-symbol-with-to-str-effects-struct-new/106661 "2007-06-13T00:37:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [June 13, 2007, 12:37am UTC](https://www.ruby-forum.com/t/extending-symbol-with-to-str-effects-struct-new/106661/1 "2007-06-13T00:37:48Z")

</div>

Not arguing either way a s to whether this makes sense or not (it’s  
had it’s lengthy discussions before).

> class Symbol  
> def to\_str  
> String(self)  
> end  
> end

But it does cause this strangeness:

> Struct.new(:x)  
> NameError: identifier x needs to be constant  
> from (irb):6:in `new’  
> from (irb):6  
> from :0

Is it really THAT unreasonable an extension? I wonder what else it  
would effect, and why. I don’t really even understand the error  
message, but one gets the same result with:

> Struct.new(‘x’)  
> NameError: identifier x needs to be constant  
> from (irb):1:in `new’  
> from (irb):1  
> from :0

That in itself seems silly.

T.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [June 13, 2007, 12:52am UTC](https://www.ruby-forum.com/t/extending-symbol-with-to-str-effects-struct-new/106661/2 "2007-06-13T00:52:49Z")

</div>

On 6/12/07, Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Struct.new(‘x’)

This is because if Struct gets a String or a “string like object  
(responds\_to? :to\_str)” it uses an alternate behavior where the first  
argument, if a proper Constant name in a string, is defined to be the  
Struct class generated under the Struct:: namespace.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [June 13, 2007, 2:06am UTC](https://www.ruby-forum.com/t/extending-symbol-with-to-str-effects-struct-new/106661/3 "2007-06-13T02:06:55Z")

</div>

On Jun 12, 6:51 pm, “Chris C.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > end  
> > > Is it really THAT unreasonable an extension? I wonder what else it
> 
> > T.
> 
> This is because if Struct gets a String or a “string like object  
> (responds\_to? :to\_str)” it uses an alternate behavior where the first  
> argument, if a proper Constant name in a string, is defined to be the  
> Struct class generated under the Struct:: namespace.

Okay. Thanks. I get the error now. Though, I don’t really get how this  
alternate behavior is useful. Why would one want to use the Struct  
namespace? And if you did, what’s wrong with

Struct::MyStruct = Struct.new( … )

But the main thing, differentiating functionality based on String vs.  
Symbol is just generally a bad idea. And very bad in core libs, IMHO.

T.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [June 13, 2007, 5:33am UTC](https://www.ruby-forum.com/t/extending-symbol-with-to-str-effects-struct-new/106661/4 "2007-06-13T05:33:10Z")

</div>

Hi,

In message “Re: Extending Symbol with #to\_str effects Struct.new”  
on Wed, 13 Jun 2007 09:05:54 +0900, Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
writes:

|Okay. Thanks. I get the error now. Though, I don’t really get how this  
|alternate behavior is useful. Why would one want to use the Struct  
|namespace?

Historical Reason. Compatibility caused this.

```
          matz.

```

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [June 13, 2007, 7:19am UTC](https://www.ruby-forum.com/t/extending-symbol-with-to-str-effects-struct-new/106661/5 "2007-06-13T07:19:19Z")

</div>

Hi,

At Wed, 13 Jun 2007 07:51:59 +0900,  
Chris C. wrote in [ruby-talk:255405]:

> This is because if Struct gets a String or a “string like object  
> (responds\_to? :to\_str)” it uses an alternate behavior where the first  
> argument, if a proper Constant name in a string, is defined to be the  
> Struct class generated under the Struct:: namespace.

You may know, it can be disabled by passing nil as the first  
argument, so that anonymous Struct will be created.
