Pretty self explanatory.
I use array_name.first to get the first element. How do you extend it to get second, third, random, etc?
Thanks.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
I discovered it is already there, in Ruby 1.9.2 at least.
If it weren’t there, I would create a file in config/initializers called array_helper.rb (or whatever) and add the following code:
Why? Because all classes in Ruby are open and you can extend anything you want.
Here’s some tricks to know:
When working in the console, if you want to test the file, be sure to use
rails consoleand notirb. Also, doreload!after creating or updating the above file. Finally, all objects must be recreated to use the new code afterreload!.