Problem with Array.rand

I’m using rvm on Mac OS X Snow Leopard.
ree-1.8.7-head with rails 2.3.8:

ree-1.8.7-head > require ‘rubygems’
=> true
ree-1.8.7-head > require ‘active_support’
=> true
ree-1.8.7-head > [1,2,3].rand
DEPRECATION WARNING: Array#rand is deprecated and will be removed in
Rails 3. Use “random_element” instead. (called from irb_binding at
(irb):6)
=> 2

That’s OK. Now ruby-1.9-2-p0 with rails 3.0.1:

ruby-1.9.2-p0 > require ‘rubygems’
=> true
ruby-1.9.2-p0 > require ‘active_support’
=> true
ruby-1.9.2-p0 > [1,2,3].rand
NoMethodError: private method rand' called for [1, 2, 3]:Array from (irb):3 from /Users/yumitsu/.rvm/rubies/ruby-1.9.2-p0/bin/irb:17:in
ruby-1.9.2-p0 > [1,2,3].random_element
NoMethodError: undefined method random_element' for [1, 2, 3]:Array from (irb):4 from /Users/yumitsu/.rvm/rubies/ruby-1.9.2-p0/bin/irb:17:in

BUT:

Loading development environment (Rails 3.0.1)
ruby-1.9.2-p0 > [1,2,3].rand
=> 1

I just don’t get it. Any ideas?

On Fri, Nov 5, 2010 at 10:22 AM, Alex Zinchenko
[email protected]wrote:

(irb):6)
from (irb):3
=> 1

I just don’t get it. Any ideas?

Since you’re using 1.9, why not just use Array#sample?


Erol M. Fornoles
http://github.com/Erol
http://twitter.com/erolfornoles
http://ph.linkedin.com/in/erolfornoles

Erol F. wrote in post #959483:

Since you’re using 1.9, why not just use Array#sample?


Erol M. Fornoles
http://github.com/Erol
http://twitter.com/erolfornoles
http://ph.linkedin.com/in/erolfornoles

Array#sample is OK, but what about backward compatibility?

On Fri, Nov 5, 2010 at 10:35 AM, Alex Zinchenko
[email protected]wrote:

Array.sample is OK, but what about backward compatibility?

Array#rand should be fine if you’re only using it inside a Rails app
(and
not with a decoupled ActiveSupport).

Btw, you might be interested in this:
https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/4555


Erol M. Fornoles
http://github.com/Erol
http://twitter.com/erolfornoles
http://ph.linkedin.com/in/erolfornoles

Erol F. wrote in post #959486:

Array#rand should be fine if you’re only using it inside a Rails app
(and
not with a decoupled ActiveSupport).

Btw, you might be interested in this:
https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/4555


Erol M. Fornoles
http://github.com/Erol
http://twitter.com/erolfornoles
http://ph.linkedin.com/in/erolfornoles

Thank you, Erol! Now I understood - they removed both Array#rand and
Array#random_element since Array#sample presents in stdlib.

Thank you again.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs