Nuby question about symbols

Why? Speed? I imagine that, in both cases, a string could have been
used as well. A string, though, would let you craft an invalid method

name.

That's evil, but apparently legal:

irb(main):005:0> $sm = "invalid name".to_sym
=> :"invalid name"
irb(main):011:0> class Foo; end
=> nil
irb(main):013:0> Foo.send :define_method, $sm, proc{"a ha!"}
=> #<Proc:0x02ec3668@(irb):13>
irb(main):016:0> Foo.instance_methods.sort - Class.methods
=> ["invalid name"]
irb(main):017:0> Foo.new."invalid name"
SyntaxError: compile error
(irb):17: syntax error
Foo.new."invalid name"
         ^
        from (irb):17
irb(main):018:0> Foo.new.send "invalid name"
=> "a ha!"

···

from :0

Mehr, Assaph (Assaph) wrote:

Why? Speed? I imagine that, in both cases, a string could have been used as well. A string, though, would let you craft an invalid method

name.

That's evil, but apparently legal:

irb(main):005:0> $sm = "invalid name".to_sym
=> :"invalid name"

You can also construct symbols with spaces as literals:

:"a b c"
# => :"a b c"
%s{a b c}
# => :"a b c"

Mehr, Assaph (Assaph) wrote:

Why? Speed? I imagine that, in both cases, a string could have been used as well. A string, though, would let you craft an invalid method

name.

That's evil, but apparently legal:

Evil, you say?

Think of the possibilities ...

:slight_smile:

James

Mehr, Assaph (Assaph) wrote:
>>Why? Speed? I imagine that, in both cases, a string could have been
>>used as well. A string, though, would let you craft an invalid method

[...]

>That's evil, but apparently legal:

Evil, you say?

Think of the possibilities ...

class << self; define_method("foo\0evil"){ puts "foo" } end

=> #<Proc:0x401fe928@(irb):1>

send("foo\0evil")

foo
=> nil

class << self; instance_methods(false) end.sort.grep(/foo/)

=> ["foo"]

Not evil anymore :slight_smile:

···

On Mon, Sep 20, 2004 at 03:11:47PM +0900, James Britt wrote:

--
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com