Extending Symbol with #to_str effects Struct.new

T

Trans

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.
 
C

Chris Carter

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


But it does cause this strangeness:

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:

NameError: identifier x needs to be constant
from (irb):1:in `new'
from (irb):1
from :0

That in itself seems silly.

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.
 
T

Trans

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.
 
N

Nobuyoshi Nakada

Hi,

At Wed, 13 Jun 2007 07:51:59 +0900,
Chris Carter 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.
 

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
474,261
Messages
2,571,308
Members
47,967
Latest member
pakasi

Latest Threads

Top