Forum: Ruby Valid name for a method

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
German Monfort (Guest)
on 2007-04-28 22:48
(Received via mailing list)
Hi all,

I'm trying to add a new method to a class but I want to name it like
`<<<' or
`++', etc but it seems that these characters are not allowed

irb(main):001:0> class A
irb(main):002:1> def <<< arg
irb(main):003:2> end
irb(main):004:1> end
SyntaxError: compile error
(irb):2: syntax error, unexpected '<', expecting '\n' or ';'
def <<< arg
       ^
(irb):4: syntax error, unexpected kEND, expecting $end
        from (irb):4
        from :0

Is there a workaround to have a `<<<' method so I can use it like a <<<
b or
even a.<<< b?
unknown (Guest)
on 2007-04-28 23:11
(Received via mailing list)
On Sun, 29 Apr 2007, German Monfort wrote:

> (irb):2: syntax error, unexpected '<', expecting '\n' or ';'
> def <<< arg
>       ^
> (irb):4: syntax error, unexpected kEND, expecting $end
>        from (irb):4
>        from :0
>
> Is there a workaround to have a `<<<' method so I can use it like a <<< b or
> even a.<<< b?
>
>

harp:~ > cat a.rb
class C
   define_method('<<<'){ 42 }
end

p C.new.send('<<<')

harp:~ > ruby a.rb
42


-a
This topic is locked and can not be replied to.