Forum: Ruby on Rails Ruby Arrays - Passing arrays from a function possible?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2c2f3ce7125411ff78a1d3e902aa2a4d?d=identicon&s=25 Bob Sanders (adistarmid)
on 2008-12-11 06:28
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?
A1b97dafea62b11e7b3bef338ce3fe83?d=identicon&s=25 Billy Hsu (cfc)
on 2008-12-11 06:38
(Received via mailing list)
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@andreas-s.net> wrote:

> end
> >
>


--
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
2c2f3ce7125411ff78a1d3e902aa2a4d?d=identicon&s=25 Bob Sanders (adistarmid)
on 2008-12-11 06:49
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?)
59528506e6297141161afcde91d677c9?d=identicon&s=25 Nicolai Reuschling (codeblogger)
on 2008-12-11 08:28
(Received via mailing list)
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
661ae6e97d0e16c2af2aaf7d197e8387?d=identicon&s=25 Sijo k g (sijo)
on 2008-12-11 08:54
Hi
  Can u try like

def product_attributes
 ['product', 12, 'test description', 'Red', 100]
end
def add_item
 product_attributes
end

Sijo
2c2f3ce7125411ff78a1d3e902aa2a4d?d=identicon&s=25 Bob Sanders (adistarmid)
on 2008-12-11 10:09
I appreciate the help Nicolai and Sijo! You both are awesome! That makes
so much more sense now. Thank you!!
9a2a53db8e9b4476038c94a64b32833f?d=identicon&s=25 Ryan Bigg (ryan-bigg)
on 2008-12-11 10:14
(Received via mailing list)
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
2c2f3ce7125411ff78a1d3e902aa2a4d?d=identicon&s=25 Bob Sanders (adistarmid)
on 2008-12-11 23:14
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.
10f7e00c1c5c60e2173995eb16f4ffc7?d=identicon&s=25 pepe (Guest)
on 2008-12-12 04:35
(Received via mailing list)
You can check this for a quick reference:

http://www.ruby-doc.org/docs/ProgrammingRuby/html/...

Pepe

On Dec 11, 5:14 pm, Bob Sanders <rails-mailing-l...@andreas-s.net>
0c778c3d6edafa40caf9128b015f0fee?d=identicon&s=25 Lewisou (Guest)
on 2008-12-12 08:51
(Received via mailing list)
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...@andreas-s.net>
26a81e5badb9e002ab9ed3542036e584?d=identicon&s=25 Michael Libby (Guest)
on 2008-12-12 08:51
(Received via mailing list)
On Thu, Dec 11, 2008 at 4:14 PM, Bob Sanders
<rails-mailing-list@andreas-s.net> 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-idio...

-Michael

--
Michael C. Libby
www.mikelibby.com
This topic is locked and can not be replied to.