Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

active_support/core_ext/array/random_access.rb conflicts with standard Ruby library

#4555

Hello,

The rand() method is overriden for Array unsafely in active_support. rand() is originally a private method taking a single parameter, but active_support adds a public rand() with no parameters. This causes some trouble with extensions of Array that want to use this method, such as the rand gem. A revised random_access.rb with a safe rand() is included.

Thanks,
djbob

Reported by djbob · May 8th, 2010 @ 08:49 AM

State: resolved
Milestone: 3.0.2
Assigned to: Xavier Noria Xavier Noria
Importance: Low

Activity

  1. djbob
    djbob

    Ermm, probably change that ">" into a ">=" in the included file.

    May 8th, 2010 @ 08:50 AM

  2. Dan Pickett
    Dan Pickett
    • Tag changed from active_support core_ext to active_support core_ext, bugmash

    can someone work to create a patch in accordance with http://rails.lighthouseapp.com/projects/8994/sending-patches? Other bugmashers can then verify and pass the issue over for core review.

    May 9th, 2010 @ 06:44 PM

  3. Santiago Pastorino
    Santiago Pastorino
    • Milestone cleared.
    • Tag changed from active_support core_ext, bugmash to active_support core_ext, bugmash, patch
    • State changed from new to verified
    • Assigned user set to Santiago Pastorino

    Patches for master and 2-3-stable attached

    May 10th, 2010 @ 05:30 PM

  4. Xavier Noria
    Xavier Noria

    The method Kernel#rand can be called in a procedural style. It is technically a private method of any class because it is defined that way in Kernel as a trick. The same happens with Kernel#gsub and others, you can call [].gsub, and you'll get an error message about a private method. A hack is leaking.

    I think a 3rd party library should be able to extend Array and expect a call to rand with no receiver to be calling Kernel#rand.

    Since Array#rand has nothing to do with Kernel#rand, I think that it is not a good solution to change the signature and choose the implementation based on the argument. Additionally, Kernel#rand can be called with no argument, but this is secondary, my main point is the previous one.

    I believe we should rename Array#rand instead.

    May 11th, 2010 @ 09:27 PM

  5. Rizwan Reza
    Rizwan Reza

    Anything on this one, considering Xavier's comment?

    May 16th, 2010 @ 03:04 AM

  6. Rizwan Reza
    Rizwan Reza
    • State changed from verified to open

    May 16th, 2010 @ 03:06 AM

  7. Eric Hutzelman
    Eric Hutzelman

    Anyone in favor of Array#random or Array#random_element? Is it worth the deprecation to make this kind of change?

    May 16th, 2010 @ 06:16 AM

  8. Xavier Noria
    Xavier Noria

    I prefer random_element, since random (and rand) are a bit ambiguous in my view and could also mean shuffle.

    This method has been there for a long time, I think it is more likely that Rails developers and plugins use it than 3rd party libraries are extending Array and using rand.

    Thus, I think it has to be renamed but I also think it is worth a deprecation cycle to ease migration to Rails 3. We can wait until 3.1 or something. If we just change what #rand returns that's going to be a subtle bug for people.

    May 16th, 2010 @ 09:49 AM

  9. Wijnand Wiersma
    Wijnand Wiersma

    +1 for Xaviers comment. Rename it but use a deprecation cycle.

    May 16th, 2010 @ 10:28 AM

  10. José Valim
    José Valim
    • Tag changed from active_support core_ext, bugmash, patch to active_support core_ext, patch

    May 16th, 2010 @ 10:30 AM

  11. Santiago Pastorino
    Santiago Pastorino
    • Assigned user changed from Santiago Pastorino to Xavier Noria

    patch for both master and 2-3-stable

    May 16th, 2010 @ 08:23 PM

  12. Repository
  13. Jeremy Kemper
    Jeremy Kemper
    • State changed from committed to open

    The master patch deprecates but does not fix. Since 3.0 is unreleased yet, we can remove rand there and deprecate it in 2-3-stable.

    May 16th, 2010 @ 09:47 PM

  14. Rizwan Reza
    Rizwan Reza
    • Tag changed from active_support core_ext, patch to active_support core_ext, bugmash-review, patch
    • State changed from open to verified

    The patches accommodate JK's suggestions.

    May 16th, 2010 @ 10:11 PM

  15. Rizwan Reza
  16. Rizwan Reza
    Rizwan Reza

    Please apply these patches on top of the ones above.

    May 16th, 2010 @ 10:41 PM

  17. Rizwan Reza
  18. Santiago Pastorino
    Santiago Pastorino

    Didn't understand yet why this https://rails.lighthouseapp.com/projects/8994/tickets/4555/a/522934... (The latest i did for 2-3-stable) was re done.
    For me seems ok.

    May 17th, 2010 @ 02:43 PM

  19. Xavier Noria
    Xavier Noria

    Santiago, the one for 2.3 was not applied because of the assert_deprecated block. I had it in my TODO but then the thread moved on.

    May 17th, 2010 @ 09:45 PM

  20. Rizwan Reza
  21. Santiago Pastorino
    Santiago Pastorino

    This issue could be closed now, right?

    May 18th, 2010 @ 08:08 PM

  22. Santiago Pastorino
    Santiago Pastorino
    • State changed from verified to resolved

    May 18th, 2010 @ 08:11 PM

  23. Marc-André Lafortune
    Marc-André Lafortune

    I'm surprised nobody pointed out that Array#sample is what it should be called, since that's what exists natively in Ruby 1.9. Ideally it should also support an optional parameter.

    My implementation passes RubySpec, so activesupport could adapt it: http://github.com/marcandre/backports/blob/master/lib/backports/1.8...

    Let me know if I should open a different ticket.

    May 26th, 2010 @ 06:49 AM

  24. Xavier Noria
    Xavier Noria

    Thank you very much Marc-André, I removed #random_element altogether and backported Array#sample using your implementation: http://github.com/rails/rails/commit/67a43554f153a3ddb97039b5fac305...

    June 5th, 2010 @ 12:22 AM

  25. Rizwan Reza
    Rizwan Reza
    • Tag changed from active_support core_ext, bugmash-review, patch to active_support core_ext, patch

    June 6th, 2010 @ 09:29 AM

  26. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 3.0.2
    • Importance changed from to Low

    October 15th, 2010 @ 11:01 PM

  27. teiddy
    teiddy

    Instructions: download and untar to /sites/all/modules

    also - Download and install 'Rules' module to /sites/all/modules

    Run /update.php

    Goto Admin>Build>Modules, enable "OA Single Group Login Redirect" module and allow Rules module to be activated when prompted.

    Log-out as admin and log-in as user with 1 group - page redirects as expected.

    Greyside Thank-you!

    Thank you for this information,I like it very much,Would you lik a pair of
    Pachuco Suits
    pack linen clothes
    pack suit into suit
    pant length
    pant length for men

    paul smith mens suits
    Welcome to our store,We have the best service team!

    November 30th, 2010 @ 05:53 AM