I tried to create an addition to Enumerable called choose_randomly that picks a random element from the Enumerable object. However when I define it and execute the code rand complains about the wrong number of arguments. Below is a capture of the ./script/console session. farrel@nicodemus ~/Work/GreenGreen/Code/green_green_client_application $ ./script/console Loading development environment (Rails 2.1.2)>> [1,2,3].choose_randomlyNoMethodError: undefined method `choose_randomly'' for [1, 2, 3]:Array from (irb):1>> module Enumerable >> def choose_randomly >> array_of_self = Array( self ) >> index = rand( array_of_self.size ) >> array_of_self[ index ] >> end >> end=> nil>> [1,2,3].choose_randomlyArgumentError: wrong number of arguments (1 for 0) from (irb):5:in `rand'' from (irb):5:in `choose_randomly'' from (irb):9>> [1,2,3][ rand( [1,2,3].size )]=> 1 As you can see if I execute the code myself it seems to work. If I copy that same code into irb it works perfectly. Any ideas? Farrel --~--~---------~--~----~------------~-------~--~----~ 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 30 Oct 2008, at 16:22, Farrel wrote:> > > As you can see if I execute the code myself it seems to work. If I > copy that same code into irb it works perfectly. >You''re calling Array#rand (a private method Array defines), whereas you want to call Kernel#rand. Replace rand with Kernel.rand and you should be ok Fred> Any ideas? > > Farrel > >--~--~---------~--~----~------------~-------~--~----~ 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 Oct 30, 7:05 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> You''re calling Array#rand (a private method Array defines), whereas > you want to call Kernel#rand. Replace rand with Kernel.rand and you > should be okThat was it. Thanks for the tip. I didn''t know Rails added in it''s own rand method into Array. Farrel --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Possibly Parallel Threads
- Models are not initialised correctly with ''rake test'' in Merb 0.9.1
- Question about splitting AD and fileserving (and tons of other stuff)
- converting multiple columns from POSIX* to Date
- Installing a package yet it will not work.
- reshape command is (stats) dropping instances