What is meant by << operator ???

Discussion in 'Ruby' started by Muhammad mohsin ali Ma, Jan 10, 2009.

  1. 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
    /*****************************************************/
    --
    Posted via http://www.ruby-forum.com/.
     
    Muhammad mohsin ali Ma, Jan 10, 2009
    #1
    1. Advertising

  2. Muhammad mohsin ali Ma

    Jan-Erik R. Guest

    Muhammad mohsin ali Ma schrieb:
    > 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
    > /*****************************************************/

    it appends the object "current_item" to the Array @items
     
    Jan-Erik R., Jan 10, 2009
    #2
    1. Advertising

  3. Muhammad mohsin ali Ma, Jan 10, 2009
    #3
  4. Muhammad mohsin ali Ma

    FrihD Guest

    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
     
    FrihD, Jan 10, 2009
    #4
  5. Muhammad mohsin ali Ma

    Gary Wright Guest

    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
    >


    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 Wright
     
    Gary Wright, Jan 10, 2009
    #5
  6. Muhammad mohsin ali Ma

    Tim Greer Guest

    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.
    --
    Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
    Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
    and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
    Industry's most experienced staff! -- Web Hosting With Muscle!
     
    Tim Greer, Jan 11, 2009
    #6
  7. Hi --

    On Sun, 11 Jan 2009, Tim Greer 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
    Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

    http://www.wishsight.com => Independent, social wishlist management!
     
    David A. Black, Jan 11, 2009
    #7
  8. Muhammad mohsin ali Ma

    Tim Greer Guest

    David A. Black wrote:

    > Hi --
    >
    > On Sun, 11 Jan 2009, Tim Greer 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
    >


    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.
    --
    Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
    Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
    and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
    Industry's most experienced staff! -- Web Hosting With Muscle!
     
    Tim Greer, Jan 11, 2009
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mohammed  A khader

    What is meant by Static name

    Mohammed A khader, Mar 1, 2005, in forum: VHDL
    Replies:
    2
    Views:
    1,318
    Mohammed A khader
    Mar 2, 2005
  2. Cowboy \(Gregory A. Beamer\) [MVP]

    Re: Sorry, meant ASP.NET

    Cowboy \(Gregory A. Beamer\) [MVP], Aug 17, 2004, in forum: ASP .Net
    Replies:
    7
    Views:
    599
    Shawn South
    Aug 18, 2004
  3. Shawn South

    Re: Sorry, meant ASP.NET

    Shawn South, Aug 17, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    439
    Shawn South
    Aug 17, 2004
  4. gayathri
    Replies:
    2
    Views:
    958
    Roedy Green
    Mar 16, 2006
  5. Guy Doucet
    Replies:
    7
    Views:
    461
    Guy Doucet
    Jun 5, 2005
Loading...

Share This Page