Ruby Arrays - Passing arrays from a function possible?


#1

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?


#2

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. <
removed_email_address@domain.invalid> 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


#3

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


#4

Hi
Can u try like

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

Sijo


#5

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


#6

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


#7

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.


#8

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


#9

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. removed_email_address@domain.invalid


#10

On Thu, Dec 11, 2008 at 4:14 PM, Bob S.
removed_email_address@domain.invalid 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:
http://theplana.wordpress.com/2007/03/03/ruby-idioms-the-splat-operator/

-Michael


Michael C. Libby
www.mikelibby.com


#11

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. removed_email_address@domain.invalid