Valid name for a method

G

German Monfort

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?
 
A

ara.t.howard

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?

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

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

harp:~ > ruby a.rb
42


-a
 

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

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,225
Latest member
Top Crypto Podcasts

Latest Threads

Top