What are method/attribute names that are actually illegal

B

Bob Hutchison

Hi,

The pickaxe book, on page 328 talks about naming of things in Ruby.
It says that reserved words should not be used as variable, method,
class, or module names.

The key word here is 'should', because it seems that, for example
'if' and 'def' both work as method names.

I can certainly understand the convention of not using keywords, but
what is the actual constraint? (I generating code and I would like to
know what I MUST enforce as opposed to SHOULD enforce)

Cheers,
Bob
 
B

Bob Hutchison

class Foo
def if; "if" end
def def; "def" end
def end; "end" end
end
=3D=3D>nil
Foo.new.if
=3D=3D>"if"
Foo.new.def
=3D=3D>"def"
Foo.new.end
=3D=3D>"end"
$KCODE=3D"u"
=3D=3D>"u"
class Foo
def =B9; 3.14159 end
end
=3D=3D>nil
Foo.new.=B9
=3D=3D>3.14159
class Foo
define_method("!^@$&@**)(") do "!^@$&@**)(" end
end
=3D=3D>#<Proc:0x0003a0c8@(irb):43>
Foo.new.send "!^@$&@**)("
=3D=3D>"!^@$&@**)("
class Foo
define_method("\0\0\0\0\0") do "null" end
end
=3D=3D>#<Proc:0x000b0c90@(irb):47>
Foo.new.send "\0\0\0\0\0"
=3D=3D>"null"
Foo.new.send "\0"
=3D=3D>"null"

The only restrictions on a normal "def foo() ... end" type method is
that the ruby parser has to be able to parse the method name. So
pretty much any valid identifier will work.

When using define_method, everything from the first null forward gets
stripped from the method name when it is created. So defining (or
calling, for that matter) a method named "\0\0\0\0\0" is like defining
(or calling) a method named "". That's the only *real* restriction I
can think of on method names. You can have unicode characters,
keywords, operators, etc... though some of those are probably not a
great idea.

Basically, Ruby is pretty liberal in what it accepts. If it gets
through the parser, it'll pretty much accept anything you throw at it.
This makes it pretty powerful, but (of course) it also gives you the
power to shoot yourself in the foot. Or elsewhere :)

Thanks Mark. This is very good news for my users (for now, that would =20=

be me :) I think very dangerous names can be dealt with, if only by =20
aliasing the names to something reasonable. I have no control over =20
the original names at all, so...

How does Ruby define the first character of a constant? just curious =20
since I see that =B9 cannot be the first character of a class name.

Cheers,
Bob
 
F

Florian Groß

Bob said:
How does Ruby define the first character of a constant? just curious =20
since I see that =EF=BF=BD cannot be the first character of a class nam=
e.

A-Z AFAIK. This might improve with better Unicode support so that =C3=84=C3=
=96=C3=9C=20
and other international characters are also recognized as uppercase.
 

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,181
Messages
2,570,970
Members
47,537
Latest member
BellCorone

Latest Threads

Top