I’m just going through Agile Web D. With Rails and we’re
using what I’m assuming is an assignment operator.
ex: @items << current_item
But (unless i skipped it) they didn’t really explain what that was. So
what is it exactly and why use it over what (at this point im
assuming) a regular assignment operator = could do?
I’m just going through Agile Web D. With Rails and we’re
using what I’m assuming is an assignment operator.
ex: @items << current_item
In the above context, << means to append ‘current_item’ to the end of
‘@items’ array.
Also, I would recommend getting a copy of the ‘Programming Ruby’ and/or
‘Programming Ruby 1.9’.
assuming) a regular assignment operator = could do?
It’s the append method. In the case above, it appends the current item
to @items which is (probably) an array.
Also, I would recommend getting a copy of the ‘Programming Ruby’ and/or
‘Programming Ruby 1.9’.
-Conrad
thanks for the help/recommendation. In the passed 2 week I went
through Simply Rails 2, now AWDWR, next up is Why’s (Poignant) Guide
to Ruby, then I’ll check out your suggestions.
On Wed, Aug 12, 2009 at 6:19 AM, Robert W. < [email protected]> wrote:
Sijo Kg wrote:
Hi brianp
The << operator has many different uses: A googling gave
Just a small clarification: << is not an operator, it is a method. It
has whatever “meaning” that is defined by its class.
<< is an operator as documented by Yukihiro M. aka Matz in
the “The Ruby P.ming Language”. Futhermore, in regards to a
String, Array, and IO classes, it is referred to as an append operator.
The << operator has many different uses: A googling gave
In an Array it is used to append (push) an object into a given array
In a Fixnum and a Bignum object it shifts the number bits to the
left
In an IO object it will write the object (as a string) to the IO
object.
In a String it will append the object (to_s) to the String (if the
object is a Fixnum between 0 and 255 it will convert it to the
character representation before appending it)
That means based on context it has different meanings
The << operator has many different uses: A googling gave
Just a small clarification: << is not an operator, it is a method. It
has whatever “meaning” that is defined by its class.
<< is most certainly an operator. It is also a method; all operators
are methods in Ruby. Not all methods are operators, but that’s another
syntactic issue.
On Aug 12, 4:37 pm, Marnen Laibow-Koser <rails-mailing-l…@andreas- s.net> wrote:
are methods in Ruby. Not all methods are operators, but that’s another
syntactic issue.
If we’re being pedantic, then not all operators are methods: ! is not
a method, nor is ?: and != is hardwired to be the negation of ==
(there are a few others eg &&, :: et.)
On Aug 12, 4:37�pm, Marnen Laibow-Koser <rails-mailing-l…@andreas- s.net> wrote:
are methods in Ruby. �Not all methods are operators, but that’s another
syntactic issue.
If we’re being pedantic, then not all operators are methods: ! is not
a method, nor is ?: and != is hardwired to be the negation of ==
(there are a few others eg &&, :: et.)
Ack, you’re right. I keep forgetting that unlike Smalltalk or C++, not every operator is a method call. Sorry about the error. (However, << is both.)
Yes, everything in Smalltalk is a method or commonly referred to as a
message that’s always
sent to an object. However, in C++, everything can be thought of as a
method but a method in
C++ can have further semantic meanings because it was derived from C.
For
example,
if I say write a method, one tends to think within a class using a
message
passing style
if I say write a function, one tends to think of a C style
representation
using a non-message
passing style sometimes
Next, one cannot overload the C and C++ operators within C++: ‘.’,
‘?:’,
‘sizeof’, ‘::’, and
‘.*’. Although, they are technically considered operators within the
language.
In short, I tend to think of operators as a specialized class of methods
which have been derived
from non-alphabetic characters and they have their particular usage
constraints. Also, the use of
the word, method, means semantically different things
in different languages.
On Aug 13, 2009, at 2:36 PM, Marnen Laibow-Koser wrote:
simplified use of some of the methods?
Yup. And if you’ve ever done BigDecimal arithmetic in Java – or just
about anything in Lisp – you can really appreciate being able to type
x.foo = 1 + 2 * 5
instead of
x.foo=(1.+(2.*(5)))
From Pickaxe, the following are not implemented as methods and thus
cannot be overridden:
&& Logical and' || Logicalor’
… … Range (inclusive and
exclusive)
? : Ternary if-then-else
= %= { /= -= += |= &= >>= <<= *= &&= ||= **= Assignment
defined? Check if symbol defined
not Logical negation
or and Logical composition
if unless while until Expression modifiers
begin/end Block expression
On Aug 12, 4:37�pm, Marnen Laibow-Koser <rails-mailing-l…@andreas- s.net> wrote:
are methods in Ruby. �Not all methods are operators, but that’s another
syntactic issue.
If we’re being pedantic, then not all operators are methods: ! is not
a method, nor is ?: and != is hardwired to be the negation of ==
(there are a few others eg &&, :: et.)
Ack, you’re right. I keep forgetting that unlike Smalltalk or C++, not every operator is a method call. Sorry about the error. (However, << is both.)
First, sorry for the added confusion, but it was actually constructive
since I learned something. I hadn’t really thought about how blurred the
line has become between operators and methods/functions. Back in the day
(the C days that is) it was fairly clear the difference between the two.
In OOP land not so much.
I suppose the determining factor in languages like Ruby is more in how
operators and methods are used syntactically, rather than where and how
they are actually defined by the language.
For example you could write the syntax this way:
“Foo”.<<(“bar”)
which would be the same as:
“Foo” << “bar”
So would we call “<<” in the first syntax a method, and “<<” in the
second syntax an operator?
Also given that the “<<” can have drastically different meaning based on
the class of the objects I support I think about “<<” more as a method
than an operator.
Example:
10 << 4
=> 160 (bit-wise shift left by 4)
Is there really a valid distinction between operators and methods
anymore? Isn’t the syntax essentially “syntactic sugar” allowing for
simplified use of some of the methods?
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.