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
On Tue, Aug 11, 2009 at 2:23 PM, brianp <brian.o.pearce-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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> > 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 > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Tue, 2009-08-11 at 14:23 -0700, brianp wrote:> 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?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 Taylor <conra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Also, I would recommend getting a copy of the ''Programming Ruby'' and/or > ''Programming Ruby 1.9''. > > -Conradthanks 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 <bwalton...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> It''s the append method. In the case above, it appends the current item > to @items which is (probably) an array. > > HTH, > Billalso, 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" On Aug 12, 5:21 am, brianp <brian.o.pea...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Aug 11, 2:38 pm, Conrad Taylor <conra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 <bwalton...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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.
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 -- Posted via ruby-forum.com.
Sijo Kg wrote:> Hi brianp > > The << operator has many different uses: A googling gaveJust a small clarification: << is not an operator, it is a method. It has whatever "meaning" that is defined by its class. -- Posted via ruby-forum.com.
On Wed, Aug 12, 2009 at 6:19 AM, Robert Walker < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 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> > -- > Posted via ruby-forum.com. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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, -- Marnen Laibow-Koser marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via ruby-forum.com.
Frederick Cheung 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 marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via ruby-forum.com.
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- s.net> wrote:> Frederick Cheung 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-Koserhttp://www.marnen.org > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > Posted viahttp://www.ruby-forum.com.
On Wed, Aug 12, 2009 at 10:49 AM, brianp <brian.o.pearce-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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. > > On Aug 12, 9:40 am, Marnen Laibow-Koser <rails-mailing-l...@andreas- > s.net> wrote: > > Frederick Cheung 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-Koserhttp://www.marnen.org > > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.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 <mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org>> > -- > > Posted viahttp://www.ruby-forum.com. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Marnen Laibow-Koser wrote:> Frederick Cheung 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? -- Posted via ruby-forum.com.
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, -- Marnen Laibow-Koser marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via ruby-forum.com.
On Aug 13, 2009, at 2:36 PM, Marnen Laibow-Koser wrote:> > 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