smallseo.info

ruby

The Ruby Programming Language

How to get a random number in Ruby?

In Ruby, how do you generate a random number between 0 and n?


Source: (StackOverflow)

What is attr_accessor in Ruby?

I am having a hard time understanding attr_accessors in Ruby, can someone explain them to me? I have done tons of Google searches, just can't understand them fully.


Source: (StackOverflow)

Why is it bad style to `rescue Exception => e` in Ruby?

Ryan Davis’s Ruby QuickRef says (without explanation):

Don’t rescue Exception. EVER. or I will stab you.

Why not? What’s the right thing to do?


Source: (StackOverflow)

class << self idiom in Ruby

What does class << self do in Ruby?


Source: (StackOverflow)

Multi-Line Comments in Ruby?

How can I comment multiple lines in Ruby?


Source: (StackOverflow)

Checking if a variable is defined?

How can I check whether a variable is defined in Ruby? Is there an isset-type method available?


Source: (StackOverflow)

How to sum array of numbers in Ruby?

I have an array of integers.

For example:

array = [123,321,12389]

Is there any nice way to get the sum of them?

I know, that

sum = 0
array.each { |a| sum+=a }

would work.


Source: (StackOverflow)

What is the difference between include and require in Ruby?

My question is similar to "What is the difference between include and extend in Ruby?".

What's the difference between require and include in Ruby? If I just want to use the methods from a module in my class, should I require it or include it?


Source: (StackOverflow)

How do I pick randomly from an array?

I want to know if there is a much cleaner way of doing this. Basically, I want to pick a random element from an array of variable length. Normally, I would do it like this:

myArray = ["stuff", "widget", "ruby", "goodies", "java", "emerald", "etc" ]
item = myArray[rand(myarray.length)]

Is there something that is more readable / simpler to replace the second line? Or is that the best way to do it. I suppose you could do myArray.shuffle.first, but I only saw #shuffle a few minutes ago on SO, I haven't actually used it yet.


Source: (StackOverflow)

Ruby function to remove all white spaces?

What is the Ruby function to remove all white space? Kind of like php's trim()?


Source: (StackOverflow)

How can I write a switch statement in Ruby?

How do I write a switch statement in Ruby?


Source: (StackOverflow)

How do I pass command line arguments to a rake task?

I've got a rake task that I am making that needs to insert a value into multiple databases.

I'd like to be able to pass this value into the rake task from the command line, or from another rake task, how can I do this?


Source: (StackOverflow)

Check if a value exists in an array in Ruby

Suppose I have a value 'Dog' and an array ['Cat', 'Dog', 'Bird']. How do I check if it exists in the array without looping through it? Is there a simple way of checking if the value exists, nothing more?


Source: (StackOverflow)

How can I remove RVM (Ruby Version Manager) from my system?

How can I remove RVM (Ruby Version Manager) from my system?


Source: (StackOverflow)

Equivalent of "continue" in Ruby

In C and many other languages, there is a continue keyword that, when used inside of a loop, jumps to the next iteration of the loop. Is there any equivalent of this continue keyword in Ruby?


Source: (StackOverflow)