Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5838221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:26:41+00:00 2026-05-22T11:26:41+00:00

I wish to make my code a little more readable by calling #rando on

  • 0

I wish to make my code a little more readable by calling #rando on any array and retrieve a random element (rando because a rand() method already exists and I don’t want there to be any confusion).

So I opened up the class and wrote a method:

class Array
    def rando
       self[ rand(length) ]
    end
end

This seems far too straightforward.

When I open up irb, and type arr = %w(hi bye) and then arr.rando I get either hi or bye back. That’s expected. However, in my rails console, when I do the same thing, I get ArgumentError: wrong number of arguments (1 for 0)

I’ve been tracing Array up the rails chain and can’t figure it out. Any idea?

FWIW, I’m using rails 2.3.11 and ruby 1.8.7

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T11:26:42+00:00Added an answer on May 22, 2026 at 11:26 am

    Works fine in my case :

    Loading development environment (Rails 3.0.3)
    ruby-1.9.2-p180 :001 > class Array
    ruby-1.9.2-p180 :002?>       def rando
    ruby-1.9.2-p180 :003?>            self[ rand(length) ]
    ruby-1.9.2-p180 :004?>         end
    ruby-1.9.2-p180 :005?>   end
     => nil 
    ruby-1.9.2-p180 :006 > arr = %w(hi bye)
     => ["hi", "bye"] 
    ruby-1.9.2-p180 :007 > arr.rando
     => "bye"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wish to make a custom kernel, that loads and executes code (my code
I wish to make my javascript more more modular by splitting it up into
I wish to make sure that my data has a constraint the following check
I have a large system that I have coded and I wish to make
I am using jQuery, and wish to make certain links open in a new
I have some REST web services implemented in WCF. I wish to make these
I'm working on my high-school matriculation asp.net project, and I wish to make a
In C# I sometimes wish I could make special methods for certain instantiations of
I can use the following code for tiny little queries: DECLARE @sql VARCHAR(8000) SET
I am using SAS 9.1 and wish to make my toolbar available regardless of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.