felipekk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Dec-11 05:51 UTC
Hash as arguments of a method call
Hi all, I''m writing a new class, and in the initialize I have: class Statement def initialize(*args) @args = args[0] @args[:start_at] = 30.days.ago if @args[:start_at].nil? @args[:finish_at] = Date.today if @args[:finish_at].nil? @customer = Customer.find(@args[:customer_id]) end (...) end This allows me, as you can see, to use @args[:customer_id] for example to read arguments passed to the initialize. This is helpful in case I need to add some more arguments to my method without breaking existing calls. This code is currently working (did not test it very long). My question is: Why do I have to do the @args = args[0] thingy? I mean, I noticed that args was an Array instead of a hash, so thats why I did it. Is this correct? It doesnt look good... Thanks! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
The arguments are passed in as an array due to your specification of *args. If you don''t want to args[0] specify: def initialize(args) And then you will be able to access it as args[:start_at] On Dec 11, 2007 4:21 PM, felipekk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <felipekk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi all, > > I''m writing a new class, and in the initialize I have: > class Statement > def initialize(*args) > @args = args[0] > @args[:start_at] = 30.days.ago if @args[:start_at].nil? > @args[:finish_at] = Date.today if @args[:finish_at].nil? > @customer = Customer.find(@args[:customer_id]) > end > > (...) > > end > > This allows me, as you can see, to use @args[:customer_id] for example > to read arguments passed to the initialize. This is helpful in case I > need to add some more arguments to my method without breaking existing > calls. > > This code is currently working (did not test it very long). > > My question is: Why do I have to do the @args = args[0] thingy? I > mean, I noticed that args was an Array instead of a hash, so thats why > I did it. Is this correct? It doesnt look good... > > Thanks! > > >-- Ryan Bigg http://www.frozenplague.net --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
felipekk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Dec-11 06:07 UTC
Re: Hash as arguments of a method call
I just read somewhere on AWDWR that the * makes it so that it accepts every argument passed, generating an array. So I thought that if I used method(args) it would accept only one arg o.O Thanks once again Ryan! On Dec 10, 10:00 pm, "Ryan Bigg" <radarliste...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> The arguments are passed in as an array due to your specification of *args. > If you don''t want to args[0] specify: > > def initialize(args) > > And then you will be able to access it as > > args[:start_at] > > On Dec 11, 2007 4:21 PM, felip...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <felip...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > Hi all, > > > I''m writing a new class, and in the initialize I have: > > class Statement > > def initialize(*args) > > @args = args[0] > > @args[:start_at] = 30.days.ago if @args[:start_at].nil? > > @args[:finish_at] = Date.today if @args[:finish_at].nil? > > @customer = Customer.find(@args[:customer_id]) > > end > > > (...) > > > end > > > This allows me, as you can see, to use @args[:customer_id] for example > > to read arguments passed to the initialize. This is helpful in case I > > need to add some more arguments to my method without breaking existing > > calls. > > > This code is currently working (did not test it very long). > > > My question is: Why do I have to do the @args = args[0] thingy? I > > mean, I noticed that args was an Array instead of a hash, so thats why > > I did it. Is this correct? It doesnt look good... > > > Thanks! > > -- > Ryan Bigghttp://www.frozenplague.net--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
You misinterpreted what it meant. *args stores all the arguments as an array, but if you pass in a hash the hash is interpreted as just one argument, so you''re able to specify many keys within the hash with it being just the one argument. On Dec 11, 2007 4:37 PM, felipekk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <felipekk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I just read somewhere on AWDWR that the * makes it so that it accepts > every argument passed, generating an array. > > So I thought that if I used method(args) it would accept only one arg > o.O > > Thanks once again Ryan! > > On Dec 10, 10:00 pm, "Ryan Bigg" <radarliste...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > The arguments are passed in as an array due to your specification of > *args. > > If you don''t want to args[0] specify: > > > > def initialize(args) > > > > And then you will be able to access it as > > > > args[:start_at] > > > > On Dec 11, 2007 4:21 PM, felip...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <felip...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > > > > > > > Hi all, > > > > > I''m writing a new class, and in the initialize I have: > > > class Statement > > > def initialize(*args) > > > @args = args[0] > > > @args[:start_at] = 30.days.ago if @args[:start_at].nil? > > > @args[:finish_at] = Date.today if @args[:finish_at].nil? > > > @customer = Customer.find(@args[:customer_id]) > > > end > > > > > (...) > > > > > end > > > > > This allows me, as you can see, to use @args[:customer_id] for example > > > to read arguments passed to the initialize. This is helpful in case I > > > need to add some more arguments to my method without breaking existing > > > calls. > > > > > This code is currently working (did not test it very long). > > > > > My question is: Why do I have to do the @args = args[0] thingy? I > > > mean, I noticed that args was an Array instead of a hash, so thats why > > > I did it. Is this correct? It doesnt look good... > > > > > Thanks! > > > > -- > > Ryan Bigghttp://www.frozenplague.net > > >-- Ryan Bigg http://www.frozenplague.net --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 11 Dec 2007, at 05:51, felipekk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> > Hi all, > > I''m writing a new class, and in the initialize I have: > class Statement > def initialize(*args) > @args = args[0] > @args[:start_at] = 30.days.ago if @args[:start_at].nil? > @args[:finish_at] = Date.today if @args[:finish_at].nil? > @customer = Customer.find(@args[:customer_id]) > end > > (...) > > end> This code is currently working (did not test it very long). > > My question is: Why do I have to do the @args = args[0] thingy? I > mean, I noticed that args was an Array instead of a hash, so thats why > I did it. Is this correct? It doesnt look good... >That''s what *args does: it collects all the arguments into an array. If your method takes as a single argument a hash of options, then why bother with the *args ? Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
what you define here is a variable length argument list you get all argument passed as an array, not a hash you could call it like: initialize({:a=>1,:b=>2},{:a=>3,:b=>4}) and get an array with two hashes to pass a single hash, you can use a simple parameter def initialize(args_hash) call it with the hash initialize({:start_at => ''whatever, :finish_at => ''now''}) or initialize(:start_at => ''whatever, :finish_at => ''now'') since you can omit the curly braces for the last or only hash argument -- 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 -~----------~----~----~----~------~----~------~--~---