[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Arrow operator with dash instead of equals (->

Andrew Green

4/22/2007 10:55:00 PM

Hi, all,

Is it possible to use -> as a method name in Ruby?

This code:

class A
def ->(arg)
puts arg
end
end

foo = A.new
foo -> "dummy_argument"

Produces:

~/test2.rb:2: syntax error, unexpected '>', expecting '\n' or
';'
def ->(arg)
^
~/test2.rb:5: syntax error, unexpected kEND, expecting $end

But if I substitute the '->' with '>>', the program works and I get:

dummy_argument

Any idea what's going on here?

Running ruby 1.8.6 on Debian unstable.

Many thanks in advance, greetings,
Andrew Green


15 Answers

Tim Hunter

4/22/2007 11:55:00 PM

0

Andrew Green wrote:
> Hi, all,
>
> Is it possible to use -> as a method name in Ruby?
>
>
No. A subset of Ruby's operators are implemented as methods. '>>' is one
of those operators. '->' isn't a Ruby operator at all.

The operators that are implemented as methods are: [], []=, **, !, ~, +,
-, *, /, %, <<, >>,& ^, |, <=>, <=, <, >, >=, ==, ===, and =~.

Andrew Green

4/23/2007 12:15:00 AM

0

> > Is it possible to use -> as a method name in Ruby?
> >
> >
> No. A subset of Ruby's operators are implemented as methods. '>>' is one
> of those operators. '->' isn't a Ruby operator at all.
>
> The operators that are implemented as methods are: [], []=, **, !, ~, +,
> -, *, /, %, <<, >>,& ^, |, <=>, <=, <, >, >=, ==, ===, and =~.
>

So is there no way to make '->' an operator and define its behavior?

If not, what about '=>'?


Tim Hunter

4/23/2007 12:47:00 AM

0

Andrew Green wrote:
>>> Is it possible to use -> as a method name in Ruby?
>>>
>>>
>>>
>> No. A subset of Ruby's operators are implemented as methods. '>>' is one
>> of those operators. '->' isn't a Ruby operator at all.
>>
>> The operators that are implemented as methods are: [], []=, **, !, ~, +,
>> -, *, /, %, <<, >>,& ^, |, <=>, <=, <, >, >=, ==, ===, and =~.
>>
>>
>
> So is there no way to make '->' an operator and define its behavior?
>
> If not, what about '=>'?
>
>
>
I listed the Ruby operators that are implemented as methods. '=>' isn't
in the list, is it?

Now, for completeness' sake, it actually is possible to define a method
named '->', but the only way you could call it would be by using 'send',
and I'm guessing you don't want to do that. It would look like this:

o.send('->', args)



Gary Wright

4/23/2007 12:52:00 AM

0


On Apr 22, 2007, at 8:15 PM, Andrew Green wrote:
> o is there no way to make '->' an operator and define its behavior?

No. Ruby does not support user defined operators.

> If not, what about '=>'?

No, '=>' is not an operator in Ruby.


Gary Wright

4/23/2007 12:55:00 AM

0


On Apr 22, 2007, at 7:54 PM, Timothy Hunter wrote:
> No. A subset of Ruby's operators are implemented as methods. '>>'
> is one of those operators. '->' isn't a Ruby operator at all.
>
> The operators that are implemented as methods are: [], []=, **, !,
> ~, +, -, *, /, %, <<, >>,& ^, |, <=>, <=, <, >, >=, ==, ===, and =~.

! is not a re-definable operator.



Gary Wright




Brian Candler

4/23/2007 11:37:00 AM

0

On Mon, Apr 23, 2007 at 09:47:14AM +0900, Timothy Hunter wrote:
> Now, for completeness' sake, it actually is possible to define a method
> named '->', but the only way you could call it would be by using 'send',
> and I'm guessing you don't want to do that. It would look like this:
>
> o.send('->', args)

This can lead to smiley programming. I recently found myself writing

o.send(:[]=, *args)

It's shorter than

o[args[0..-2]] = args[-1]

Caleb Clausen

4/23/2007 2:44:00 PM

0

It's not possible to define a -> operator in Ruby, but you can
(sometimes) define <-. Watch:

class Right
def -@
return Negated.new(self)
end

class Negated
def initialize(right)
@inner=right
end

attr :inner
end
end

class Left
def <(negated_right)
self.inspect + " <- " + negated_right.inner.inspect
end
end

Left.new <- Right.new #=> "#<Left:0x403cf68c> <- #<Right:0x403cf678>"

It's actually two operators, so there's a caveat: if the right-side
class already defines unary minus, this won't work.

(I first read about this trick in a C++ book. I'm not necessarily
recommending it.)

Joel VanderWerf

4/23/2007 4:11:00 PM

0

Brian Candler wrote:
> On Mon, Apr 23, 2007 at 09:47:14AM +0900, Timothy Hunter wrote:
>> Now, for completeness' sake, it actually is possible to define a method
>> named '->', but the only way you could call it would be by using 'send',
>> and I'm guessing you don't want to do that. It would look like this:
>>
>> o.send('->', args)
>
> This can lead to smiley programming. I recently found myself writing
>
> o.send(:[]=, *args)
>
> It's shorter than
>
> o[args[0..-2]] = args[-1]

:-D very nice!

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Andrew Green

4/23/2007 5:26:00 PM

0

El lun, 23-04-2007 a las 09:52 +0900, Gary Wright escribió:
> > o is there no way to make '->' an operator and define its behavior?
>
> No. Ruby does not support user defined operators.
>
> > If not, what about '=>'?
>
> No, '=>' is not an operator in Ruby.
>
>

Many thanks for your replies, everyone...

I'll explain briefly why I'm asking:

We're working on a domain-specific language that is mainly declarative
but that includes little snippets of executable code. These snippets
are, at least in our first implementation, in Ruby.

In some of the non-executable parts of the language we declare rules for
traversing segments of a graph using an arror (->). For example:

author -> place_of_birth

or

sibbling -> children -> age

It would be really, really nice to be able to use this same syntax in
the executable Ruby snippets, more or less along the lines of:

(x -> last_names).to_s + ", " + (x -> first_names).to_s

Using a dot instead of an arrow here is not an option for various
reasons.

Any ideas as to how to hack around this limitation in Ruby, then?

Our interpreter eventually gets the code blocks as strings, so I could
just substitute the -> for >> behind the user's back, but that would be
messy, because it would substitute all occurrences of ->, even if
they're in quotes or interpretable by Ruby in some other, unforseen way.
Or is there some clean way of doing this? With ri or something similar,
maybe?

Thanks again,
Andrew

----------

(For anyone curious about what we're doing here, here are some links (in
Spanish):
http://200.67.231.185/mediawiki/index.php/Pescador:Recursos_para_desar...
http://durito.n...
)




Brian Candler

4/23/2007 7:31:00 PM

0

On Tue, Apr 24, 2007 at 02:25:33AM +0900, Andrew Green wrote:
> (x -> last_names).to_s + ", " + (x -> first_names).to_s
>
> Using a dot instead of an arrow here is not an option for various
> reasons.
>
> Any ideas as to how to hack around this limitation in Ruby, then?

As you say, you could use >>, although it has a fairly low operator
precedence, so

x >> last_names + x >> first_names

would be parsed, I think, as

x >> (last_names + x) >> first_names

If you're going to run through a preprocessor to substitute ->, then "." may
be a better substitution.

> Our interpreter eventually gets the code blocks as strings, so I could
> just substitute the -> for >> behind the user's back, but that would be
> messy, because it would substitute all occurrences of ->, even if
> they're in quotes or interpretable by Ruby in some other, unforseen way.
> Or is there some clean way of doing this? With ri or something similar,
> maybe?

The cleanest way I suspect would be to define a grammar for your own
language, and write a parser for it. I believe there's a Ruby parser
generator out there.

Then you are not bound by the rules of Ruby syntax at all.

Regards,

Brian.