# What is meant by \<\< operator?

**URL:** <https://www.ruby-forum.com/t/what-is-meant-by-operator/157404>\
**Category:** Ruby\
**Created:** [January 10, 2009, 8:07pm UTC](https://www.ruby-forum.com/t/what-is-meant-by-operator/157404 "2009-01-10T20:07:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Muhammad\_mohsin\_ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/73ab20/32.png) [@Muhammad\_mohsin\_ali](https://www.ruby-forum.com/u/Muhammad_mohsin_ali)\
**Post date:** [January 10, 2009, 8:07pm UTC](https://www.ruby-forum.com/t/what-is-meant-by-operator/157404/1 "2009-01-10T20:07:24Z")

</div>

What is meant by \<\< operator?

For example this operator is used in following code,

/_**/  
def add\_product(product)  
current\_item = @items.find {|item| item.product == product}  
if current\_item  
current\_item.increment\_quantity  
else  
current\_item = CartItem.new(product)  
 @items \<\< current\_item  
end  
current\_item  
end  
/**_/

---

<div class="post-metadata">

**Author:** ![Muhammad\_mohsin\_ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/73ab20/32.png) [@Muhammad\_mohsin\_ali](https://www.ruby-forum.com/u/Muhammad_mohsin_ali)\
**Post date:** [January 10, 2009, 8:12pm UTC](https://www.ruby-forum.com/t/what-is-meant-by-operator/157404/2 "2009-01-10T20:12:11Z")

</div>

Muhammad mohsin ali Ma schrieb:

> else  
> current\_item = CartItem.new(product)  
> @items \<\< current\_item  
> end  
> current\_item  
> end  
> /\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/  
> it appends the object “current\_item” to the Array @items

---

<div class="post-metadata">

**Author:** ![Muhammad\_mohsin\_ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/73ab20/32.png) [@Muhammad\_mohsin\_ali](https://www.ruby-forum.com/u/Muhammad_mohsin_ali)\
**Post date:** [January 10, 2009, 8:51pm UTC](https://www.ruby-forum.com/t/what-is-meant-by-operator/157404/3 "2009-01-10T20:51:09Z")

</div>

Do u know right syntax of form tag?

---

<div class="post-metadata">

**Author:** ![Muhammad\_mohsin\_ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/73ab20/32.png) [@Muhammad\_mohsin\_ali](https://www.ruby-forum.com/u/Muhammad_mohsin_ali)\
**Post date:** [January 10, 2009, 9:30pm UTC](https://www.ruby-forum.com/t/what-is-meant-by-operator/157404/4 "2009-01-10T21:30:51Z")

</div>

Hello,

Muhammad mohsin ali Ma wrote:

> What is meant by \<\< operator?

It is not an operator, but actually is the method “\<\<”. So it depends on  
the object that receives this method.

> def add\_product(product)  
> current\_item = @items.find {|item| item.product == product}  
> if current\_item  
> current\_item.increment\_quantity  
> else  
> current\_item = CartItem.new(product)  
> @items \<\< current\_item  
> end  
> current\_item  
> end

Consequently given that @items certainly is an Array, it appends  
current\_item to @items which seems quite intuitive.

–Lucas

---

<div class="post-metadata">

**Author:** ![Muhammad\_mohsin\_ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/73ab20/32.png) [@Muhammad\_mohsin\_ali](https://www.ruby-forum.com/u/Muhammad_mohsin_ali)\
**Post date:** [January 11, 2009, 1:30am UTC](https://www.ruby-forum.com/t/what-is-meant-by-operator/157404/5 "2009-01-11T01:30:23Z")

</div>

FrihD wrote:

> It is not an operator, but actually is the method “\<\<”. So it depends  
> on the object that receives this method.

Actually, it depends on what it depends on, because it’s both (either  
or). It’s a method, or just an operator (\<\< left shift bitwise  
operator), or as an append operator. I suppose it’s all in the use and  
wording, though.

---

<div class="post-metadata">

**Author:** ![Muhammad\_mohsin\_ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/73ab20/32.png) [@Muhammad\_mohsin\_ali](https://www.ruby-forum.com/u/Muhammad_mohsin_ali)\
**Post date:** [January 10, 2009, 10:04pm UTC](https://www.ruby-forum.com/t/what-is-meant-by-operator/157404/6 "2009-01-10T22:04:51Z")

</div>

On Jan 10, 2009, at 3:29 PM, FrihD wrote:

> Muhammad mohsin ali Ma wrote:
> 
> > What is meant by \<\< operator?
> 
> It is not an operator, but actually is the method “\<\<”. So it  
> depends on the object that receives this method.

We just had a long rambling thread about operators vs. methods:  
\<[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/323906](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/323906)

> 

I’d describe ‘\<\<’ as a re-definable operator. Syntactically it is an  
operator but its semantics are defined by an associated method.

Ruby also has operators that can’t be redefined: || && or and not ! ?:

There are often semantics associated with operators that shouldn’t be  
disregarded without thought. For example:

lhs \<\< rhs

often means that rhs will ‘added’ to lhs modifying lhs in the  
process. On the other hand:

lhs + rhs

generally means new object formed by ‘adding’ rhs’ to ‘lhs’ will be  
returned and that lhs will not be modified.

Ruby doesn’t enforce these semantics but developers shouldn’t ignore  
them either.

Gary W.

---

<div class="post-metadata">

**Author:** ![Muhammad\_mohsin\_ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/73ab20/32.png) [@Muhammad\_mohsin\_ali](https://www.ruby-forum.com/u/Muhammad_mohsin_ali)\
**Post date:** [January 11, 2009, 2:46am UTC](https://www.ruby-forum.com/t/what-is-meant-by-operator/157404/7 "2009-01-11T02:46:24Z")

</div>

David A. Black wrote:

> > or). It’s a method, or just an operator (\<\< left shift bitwise
> 
> The syntactic sugar, however, has the clear purpose of making it look  
> like an infix operator. I think it’s an operator kind of the way  
> “attributes” are attributes – that is, mainly in the eye of the  
> beholder. The language really doesn’t care whether we call things  
> attributes and operators, so it’s all about what helps people make  
> sense of it.
> 
> David

That’s pretty much what I was saying, too. (Or trying to say). I  
personally don’t care how people refer to things, provided it conveys  
the intent and function. It’s all good to me.

---

<div class="post-metadata">

**Author:** ![Muhammad\_mohsin\_ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/73ab20/32.png) [@Muhammad\_mohsin\_ali](https://www.ruby-forum.com/u/Muhammad_mohsin_ali)\
**Post date:** [January 11, 2009, 1:42am UTC](https://www.ruby-forum.com/t/what-is-meant-by-operator/157404/8 "2009-01-11T01:42:40Z")

</div>

Hi –

On Sun, 11 Jan 2009, Tim G. wrote:

> FrihD wrote:
> 
> > It is not an operator, but actually is the method “\<\<”. So it depends  
> > on the object that receives this method.
> 
> Actually, it depends on what it depends on, because it’s both (either  
> or). It’s a method, or just an operator (\<\< left shift bitwise  
> operator), or as an append operator. I suppose it’s all in the use and  
> wording, though.

The method-ness has a certain primacy, in the sense that this:

a \<\< b

is always a method call; that is, it is always the same as:

a.\<\<(b)

The syntactic sugar, however, has the clear purpose of making it look  
like an infix operator. I think it’s an operator kind of the way  
“attributes” are attributes – that is, mainly in the eye of the  
beholder. The language really doesn’t care whether we call things  
attributes and operators, so it’s all about what helps people make  
sense of it.

David

–  
David A. Black / Ruby Power and Light, LLC  
Ruby/Rails consulting & training: [http://www.rubypal.com](http://www.rubypal.com)  
Coming in 2009: The Well-Grounded Rubyist ([The Well-Grounded Rubyist](http://manning.com/black2))

[http://www.wishsight.com](http://www.wishsight.com) =\> Independent, social wishlist management!
