Ruby Arrays - Passing arrays from a function possible?

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?

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 S. <
[email protected]> wrote:

end


TWRUG Blog:
http://blog.rubyonrails.org.tw

CFC on Rails:

Only two surfaces of a box:
http://blog.pixnet.net/zusocfc

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?)

Hi
Can u try like

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

Sijo

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

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

Ryan B. 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.

I appreciate the help Nicolai and Sijo! You both are awesome! That makes
so much more sense now. Thank you!!

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 S. [email protected]

On Thu, Dec 11, 2008 at 4:14 PM, Bob S.
[email protected] wrote:

Ryan B. 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:

-Michael


Michael C. Libby
www.mikelibby.com

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 S. [email protected]