David Kahn
2011-Jan-25 00:39 UTC
Add syntactic sugar for array ''<<'' method on my own method?
I want to write a method which takes its input and adds it to an array (in this case I am trying to abstract a session item as I think it looks prettier to use a helper method). For example, native in ruby we have arr: arr = [] arr << ''item'' Is there a way I can write a method that uses the ''<<'' syntactic sugar? Been poking around and don''t see clearly how to do this. Just like you can write a method for assignment: def assign_something=(value) -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2011-Jan-25 00:43 UTC
Re: Add syntactic sugar for array ''<<'' method on my own method?
David Kahn wrote in post #977285:> I want to write a method which takes its input and adds it to an array > (in > this case I am trying to abstract a session item as I think it looks > prettier to use a helper method). > > For example, native in ruby we have arr: > > arr = [] > arr << ''item'' > > Is there a way I can write a method that uses the ''<<'' syntactic sugar? > Been > poking around and don''t see clearly how to do this.def <<(other) # whatever end Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
David Kahn
2011-Jan-25 00:55 UTC
Re: Re: Add syntactic sugar for array ''<<'' method on my own method?
On Mon, Jan 24, 2011 at 6:43 PM, Marnen Laibow-Koser <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>wrote:> David Kahn wrote in post #977285: > > I want to write a method which takes its input and adds it to an array > > (in > > this case I am trying to abstract a session item as I think it looks > > prettier to use a helper method). > > > > For example, native in ruby we have arr: > > > > arr = [] > > arr << ''item'' > > > > Is there a way I can write a method that uses the ''<<'' syntactic sugar? > > Been > > poking around and don''t see clearly how to do this. > > def <<(other) > # whatever > end >I see, so it has to be within a class and not named.... so if I am writing a helper method in app controller then this is weird, but now I see that I need to make a class for what I do, so thanks, my design has improved.> > Best, > -- > Marnen Laibow-Koser > http://www.marnen.org > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > > -- > Posted via http://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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2011-Jan-25 01:18 UTC
Re: Re: Add syntactic sugar for array ''<<'' method on my own method?
David Kahn wrote in post #977289:> On Mon, Jan 24, 2011 at 6:43 PM, Marnen Laibow-Koser > <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>wrote: > >> > >> > Is there a way I can write a method that uses the ''<<'' syntactic sugar? >> > Been >> > poking around and don''t see clearly how to do this. >> >> def <<(other) >> # whatever >> end >> > > I see, so it has to be within a classModule, actually. But this shouldn''t surprise you: it''s just a method like any other.> and not named....It is named. Its name is :<< .> so if I am > writing > a helper method in app controller then this is weird, but now I see that > I > need to make a class for what I do, so thanks, my design has improved.Yeah. Don''t put logic in your controllers. And remember that you can reopen or mix into existing classes. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.