Assignment with << ?

Hey,

I'm just going through Agile Web Development 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?

thanks, brianp

Hey,

I’m just going through Agile Web Development 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’.

Good luck,

-Conrad

It's the append method. In the case above, it appends the current item to @items which is (probably) an array.

HTH, Bill

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.

"<<" is not only for collection class . even it also works for string

suppose veriable1 = "abc"

veriable1 << "def"

then if you print the value of veriable1 you will get "abcdef"

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.

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 Matsumoto aka Matz in

the “The Ruby Programming Language”. Futhermore, in regards to a

String, Array, and IO classes, it is referred to as an append operator.

-Conrad

Robert Walker 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 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.

Best,

Robert Walker 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 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.

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.)

Fred

Frederick Cheung wrote:

Thanks for the clarification everyone!

I wouldn't have expected it to have different meaning depending on he object/class type. I'll keep an eye out for that.

Thanks for the clarification everyone!

I wouldn’t have expected it to have different meaning depending on he

object/class type. I’ll keep an eye out for that.

Frederick Cheung 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.)

Best,

Marnen Laibow-Koserhttp://www.marnen.org

mar...@marnen.org

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.

-Conrad

Marnen Laibow-Koser wrote:

Frederick Cheung 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?

Robert Walker wrote: [...]

Is there really a valid distinction between operators and methods anymore?

Not for all operators in Ruby. In some languages, not for *any* operator

Isn't the syntax essentially "syntactic sugar" allowing for 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)))

Best,

Robert Walker wrote: [...]

Is there really a valid distinction between operators and methods anymore?

Not for all operators in Ruby. In some languages, not for *any* operator

Isn't the syntax essentially "syntactic sugar" allowing for 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'

                                           Logical `or'

.. ... 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