Assignment with <<?

Hey,

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?

thanks,
brianp

On Tue, Aug 11, 2009 at 2:23 PM, brianp [email protected]
wrote:

Hey,

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

Good luck,

-Conrad

On Tue, 2009-08-11 at 14:23 -0700, brianp wrote:

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.

HTH,
Bill

On Aug 11, 2:38 pm, Conrad T. [email protected] wrote:

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 Aug 11, 2:39 pm, bill walton [email protected] wrote:

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

HTH,
Bill

also, thanks for your help !

That makes complete sense. I had a lapse and forgot we were dealing
with a collection/array.

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

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.

-Conrad

Hi brianp

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

Sijo

Robert W. 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,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

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

Fred

Frederick C. wrote:

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

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

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.

On Aug 12, 9:40 am, Marnen Laibow-Koser <rails-mailing-l…@andreas-

On Wed, Aug 12, 2009 at 10:49 AM, brianp [email protected]
wrote:

s.net> wrote:

Best,

Marnen Laibow-Koserhttp://www.marnen.org
[email protected]

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

[email protected]

Robert W. 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,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

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

Marnen Laibow-Koser wrote:

Frederick C. wrote:

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.

| Privacy Policy | Terms of Service | Remote Ruby Jobs