VERY simple question about "?"

Brian, you are contradicting yourself: first you deny the presence of operators in Ruby and then you talk about them nevertheless. :slight_smile:

Fact is, there _are_ operators in Ruby - and they do have a precedence. (I am not sure why you put an "only" into the sentence above - operator precedence is what a precedence table is about.)

Fact is also, that their semantics are defined via methods. Note also that for understanding the semantics of most binary operators the method #coerce plays a crucial role (=> double dispatch).

Kind regards

  robert

···

On 05.01.2009 10:58, Brian Candler wrote:

There aren't really any operators in Ruby. Ruby simply maps all these to method calls (*), so to find out what the operator *means* you have to look at the class documentation for that method.

The only real purpose of the operator precedence table is to show how expressions containing multiple operators are resolved. e.g.

--
remember.guy do |as, often| as.you_can - without end

Tom Cloyd wrote:

OK, let's assume we have the language problem solved (ha! - not likely
to happen). Nann-methods == "non-alphabetically-named methods".

That's not exactly the same though. You can (with a bit of work) define
arbitrary methods with non-alphanumeric names, but they are not
operators.

The things we're talking about are called "operators" because they look,
syntactically, like operators in other languages, and are parsed as
such.

     foo + bar # infix binary plus (method +)
     - foo # prefix unary minus (method -@)

That is: "arg1 op arg2" instead of "arg1.methodname(arg2)"

The fundamental thing is that the infix/prefix notation shown above is
parsed successfully, but then translated into normal method calls. You
can then define or redefine these methods, just like any other methods.

class Foo
  def -@
    "minus #{self}"
  end
end

f = Foo.new
p -f # operator notation (unary minus)
p f.-@ # same but using explicit method call

The core
problem still remains - what the blazes does that THING
do/mean/produce...???

It produces whatever it is defined to produce :slight_smile:

That is, class Foo may or may not implement the unary minus
operator/method (or any other operator). If it does, it is the
programmer's choice as to what it does.

Equally, I could define a method Foo#xcikuhruh. It would be my choice
what it does :slight_smile:

You may have an implicit expectation of, say, what the "*" operator
does, i.e. multiplication. But it may not make sense in non-numeric
contexts. For example, the facets library defines a way to combine Proc
objects using *:

----------------------------------------------------------------- Proc#*
     *(x)

···

------------------------------------------------------------------------
     Operator for Proc#compose and Integer#times_collect/of.

       a = lambda { |x| x + 4 }
       b = lambda { |y| y / 2 }

       (a * b).call(4) #=> 6
       (b * a).call(4) #=> 4

       CREDIT: Dave

An operator precedence table shows all the operators known by Ruby, and
the ways they interact with each other in complex expressions. It
doesn't make sense to include what they "mean", because they will mean
different things when applied to different objects. And the joy is, you
can make them mean whatever you like when applied to *your* objects.

A neat trick I've seen is a pathname class which uses the '/' operator
to compose paths: e.g.

  path1 = MyPath.new("home")
  path2 = path1 / "bar" / "baz" # results in /home/bar/baz

Saying that the '/' operator means "division" doesn't make sense here.

HTH,

Brian.
--
Posted via http://www.ruby-forum.com/\.

Robert Klemme wrote:

Brian, you are contradicting yourself: first you deny the presence of
operators in Ruby and then you talk about them nevertheless. :slight_smile:

Fact is, there _are_ operators in Ruby - and they do have a precedence.
   (I am not sure why you put an "only" into the sentence above -
operator precedence is what a precedence table is about.)

Operators in Ruby are nothing more than syntactic sugar for method
calls.

The "only" was because the OP wanted the table to include the "meaning"
of each operator. I am agreeing with you that an operator precedence
table should show only operator precedence :slight_smile:

···

--
Posted via http://www.ruby-forum.com/\.

Robert Klemme wrote:

Brian, you are contradicting yourself: first you deny the presence of
operators in Ruby and then you talk about them nevertheless. :slight_smile:

Fact is, there _are_ operators in Ruby - and they do have a precedence.
   (I am not sure why you put an "only" into the sentence above -
operator precedence is what a precedence table is about.)

Operators in Ruby are nothing more than syntactic sugar for method calls.

Well, in a way that's what they are in every procedural language - they are just a special notation for a method / function invocation, aren't they? Even if technically a compiler does not generate a subroutine call, they represent the code inserted there which is always the same for the same types of arguments. What makes operators special in some procedural languages is that they are overloaded (plus for int, plus for float etc.) while functions cannot be overloaded in those languages. And, of course, they need a defined precedence because of the different syntax (i.e. their arguments are not grouped via brackets as for function / method calls).

The "only" was because the OP wanted the table to include the "meaning" of each operator. I am agreeing with you that an operator precedence table should show only operator precedence :slight_smile:

:slight_smile: Ah, I see. In case of Ruby the meaning cannot be included anyway because operators are overloaded and can be overloaded by the user - so there is no fixed meaning.

Kind regards

  robert

···

On 05.01.2009 23:21, Brian Candler wrote:

--
remember.guy do |as, often| as.you_can - without end

Robert Klemme wrote:

Operators in Ruby are nothing more than syntactic sugar for method
calls.

Well, in a way that's what they are in every procedural language - they
are just a special notation for a method / function invocation, aren't
they?

I don't think so. Well, not in C anyway. The operation "a + b" is
defined to be an operation on a and b (e.g. integer addition or float
addition). It is not defined as a function call, and it cannot be
changed by the user.

The same applies to Perl I think.

In Ruby, + is not defined in terms of its operation (i.e. what it does
with the arguments), but just that it dispatches to a method on the
left-hand argument. They really are syntactic sugar, since they parse to
exactly the same as the explicit method call syntax:

$ cat add1.rb
1 + 2 * 3
$ cat add2.rb
1.+(2.*(3))
$ parse_tree_show add1.rb
s(:call,
s(:lit, 1),
:+,
s(:array, s(:call, s(:lit, 2), :*, s(:array, s(:lit, 3)))))
$ parse_tree_show add2.rb
s(:call,
s(:lit, 1),
:+,
s(:array, s(:call, s(:lit, 2), :*, s(:array, s(:lit, 3)))))

Now, in principle a C compiler could be written such that a+b is
rewritten at the parsing stage to cc_int_add(a,b) or somesuch. But this
wouldn't be a function like a user-written one. For a start, it would
have to generate machine code or some intermediate VM code directly,
since there is no primitive '+' operator to use. Secondly, it would need
to be able to handle arguments in different combinations of registers,
so that an expression like cc_int_add(a, cc_int_mul(b,c)) doesn't spill
registers everywhere.

So cc_int_add() might *look* like a function, but actually it would be a
special node type which generates addition code. In that case, the parse
tree may as well remember that it's a "+" operator with two arguments,
since converting it to something which looks like a function call (but
isn't) doesn't help at all.

Regards,

Brian.

···

--
Posted via http://www.ruby-forum.com/\.