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.
Bob S. (Guest)
on 2008-12-11 07: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?
Billy H. (Guest)
on 2008-12-11 07: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 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
Bob S. (Guest)
on 2008-12-11 07: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?)
Nicolai R. (Guest)
on 2008-12-11 09: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
Sijo k. (Guest)
on 2008-12-11 09:54
Hi
  Can u try like

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

Sijo
Bob S. (Guest)
on 2008-12-11 11:09
I appreciate the help Nicolai and Sijo! You both are awesome! That makes
so much more sense now. Thank you!!
Ryan B. (Guest)
on 2008-12-11 11: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
Bob S. (Guest)
on 2008-12-12 00:14
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.
pepe (Guest)
on 2008-12-12 05: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 S. <removed_email_address@domain.invalid>
Lewisou (Guest)
on 2008-12-12 09: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 S. <removed_email_address@domain.invalid>
Michael L. (Guest)
on 2008-12-12 09:51
(Received via mailing list)
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-idio...

-Michael

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