I see that I''m repeating a lot of code throughout my app, and I wanted to do something like this in my model: def product_attributes [product, quantity, description, color, size] end def add_item(product_attributes) ... end ...but that of course isn''t working. (I''m getting a "wrong number of arguments (5 for 1)" error) Is there a way to pass those product attributes into the add_item function properly? Or, maybe you know of a different way I should I approach this? -- 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-/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 http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
It works on my machine. or could you paste the full code of add_item? On Thu, Dec 11, 2008 at 1:28 PM, Bob Sanders < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > I see that I''m repeating a lot of code throughout my app, and I wanted > to do something like this in my model: > > def product_attributes > [product, quantity, description, color, size] > end > > def add_item(product_attributes) > ... > end > > ...but that of course isn''t working. (I''m getting a "wrong number of > arguments (5 for 1)" error) > > Is there a way to pass those product attributes into the add_item > function properly? Or, maybe you know of a different way I should I > approach this? > -- > Posted via http://www.ruby-forum.com/. > > > >-- TWRUG Blog: http://blog.rubyonrails.org.tw CFC on Rails: http://zusocfc.blogspot.com Only two surfaces of a box: http://blog.pixnet.net/zusocfc --~--~---------~--~----~------------~-------~--~----~ 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 http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Bob Sanders
2008-Dec-11 05:49 UTC
Re: Ruby Arrays - Passing arrays from a function possible?
Thanks for the quick reply Billy. Sorry, I should''ve explained it clearer. Basically, I''m trying to add items (along with the item''s user-inputted attributes) to a cart. I''m using this in my controller: @cart.add_item(product, params[:quantity], params[:description], params[:color], params[:size]) Now, in my model, I want something like this: def add_item(product, quantity, description, color, size) ... end That''s correct, but I''m finding it tedious to constantly paste the "product, quantity, description, color, size" a bunch of times throughout my app. It''s somewhat time-consuming, but I think even more important, it''s inflexible (e.g., if I want to change or add one attribute in the future). So, my main goal is to convert this: def add_item(product, quantity, description, color, size) ... end to this: def add_item(product_attributes) ... end while still being able to use the add_item function: @cart.add_item(product, params[:quantity], params[:description], params[:color], params[:size]) What do you think? (Am I approaching this the wrong way?) -- 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-/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 http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Codeblogger
2008-Dec-11 07:27 UTC
Re: Ruby Arrays - Passing arrays from a function possible?
Hi! def product_attributes [product, quantity, description, color, size] end This returns 1 argument (an array). Your method expects 5 (product, quantity, description, color and size). Maybe you want to try this with a hash... def product_attributes {:product => product, :quantity => quantity} # and so on for all attributes end Then you''re able to do something like: def add_item(product_attributes) product = product_attributes[:product] quantity = product_attributes[:quantity] # ... end I hope this helps! Nicolai --~--~---------~--~----~------------~-------~--~----~ 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 http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Can u try like def product_attributes [''product'', 12, ''test description'', ''Red'', 100] end def add_item product_attributes end Sijo -- 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-/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 http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Bob Sanders
2008-Dec-11 09:09 UTC
Re: Ruby Arrays - Passing arrays from a function possible?
I appreciate the help Nicolai and Sijo! You both are awesome! That makes so much more sense now. Thank you!! -- 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-/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 http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ryan Bigg
2008-Dec-11 09:13 UTC
Re: Ruby Arrays - Passing arrays from a function possible?
Or I think you can do *args for the receivIng method and then call it like method(product_attributes) and that will pass the array to the method On 11/12/2008, at 17:27, Codeblogger <codeblogger-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi! > > def product_attributes > [product, quantity, description, color, size] > end > > This returns 1 argument (an array). Your method expects 5 (product, > quantity, description, color and size). Maybe you want to try this > with a hash... > > def product_attributes > {:product => product, :quantity => quantity} # and so on for all > attributes > end > > Then you''re able to do something like: > > def add_item(product_attributes) > product = product_attributes[:product] > quantity = product_attributes[:quantity] > # ... > end > > I hope this helps! > > Nicolai > > >--~--~---------~--~----~------------~-------~--~----~ 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 http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Bob Sanders
2008-Dec-11 22:14 UTC
Re: Ruby Arrays - Passing arrays from a function possible?
Ryan Bigg wrote:> Or I think you can do *args for the receivIng method and then call it > like method(product_attributes) and that will pass the array to the > methodThanks Ryan. Can you explain more about how *args would work? I''m googling for more info, but can''t seem to find a good tutorial on it. -- 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-/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 http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Michael Libby
2008-Dec-11 22:34 UTC
Re: Ruby Arrays - Passing arrays from a function possible?
On Thu, Dec 11, 2008 at 4:14 PM, Bob Sanders <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Ryan Bigg wrote: >> Or I think you can do *args for the receivIng method and then call it >> like method(product_attributes) and that will pass the array to the >> method > > Thanks Ryan. Can you explain more about how *args would work? I''m > googling for more info, but can''t seem to find a good tutorial on it.google for: ruby splat operator you feel lucky: http://theplana.wordpress.com/2007/03/03/ruby-idioms-the-splat-operator/ -Michael -- Michael C. Libby www.mikelibby.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 http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
def add_item(×product_attributes) product, quantity, description, color, size = ×product_attributes end then add_time(product, quantity, description, color, size) On Dec 11, 1:28 pm, Bob Sanders <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I see that I''m repeating a lot of code throughout my app, and I wanted > to do something like this in my model: > > def product_attributes > [product, quantity, description, color, size] > end > > def add_item(product_attributes) > ... > end > > ...but that of course isn''t working. (I''m getting a "wrong number of > arguments (5 for 1)" error) > > Is there a way to pass those product attributes into the add_item > function properly? Or, maybe you know of a different way I should I > approach this? > -- > 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
You can check this for a quick reference: http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_methods.html Pepe On Dec 11, 5:14 pm, Bob Sanders <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Ryan Bigg wrote: > > Or I think you can do *args for the receivIng method and then call it > > like method(product_attributes) and that will pass the array to the > > method > > Thanks Ryan. Can you explain more about how *args would work? I''m > googling for more info, but can''t seem to find a good tutorial on it. > -- > 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---