The Ruby Programming Language

What does "WARN Could not determine content-length of response body." mean and how to I get rid of it?

Since upgrading to Rails 3.1 I'm seeing this warning message in my development log:

WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true

What does this mean and how can I remove it? Is it a problem?

Source: (StackOverflow)

Match All Occurrences of a Regex

Is there a quick way to find every match of a regular expression in Ruby? I've looked through the Regex object in the Ruby STL and searched on Google to no avail.

Source: (StackOverflow)

How best to generate a random string in Ruby

I'm currently using the following to generate an 8 character pseudo random upper case string [A-Z]

value = ""; 8.times{value  << (65 + rand(25)).chr}

but it looks junky, and since it isn't a single statement it can't be passed as an argument. To get a mixed case string [a-zA-Z] I further hack into it with

value = ""; 8.times{value << ((rand(2)==1?65:97) + rand(25)).chr}

Just looks like trash. Anyone have a better method?

Source: (StackOverflow)

How to make --no-ri --no-rdoc the default for gem install?

I don't use the RI or RDoc output from the gems I install in my machine or in the servers I handle (I use other means of documentation).

Every gem I install installs RI and RDoc documentation by default, because I forget to set --no-ri --no-rdoc.

Is there a way to make those two flags the default?

Source: (StackOverflow)

How to run Rake tasks from within Rake tasks?

I have a Rakefile that compiles the project in two ways, according to the global variable $build_type, which can be :debug or :release (the results go in separate directories):

task :build => [:some_other_tasks] do

I wish to create a task that compiles the project with both configurations in turn, something like this:

task :build_all do
  [ :debug, :release ].each do |t|
    $build_type = t
    # call task :build with all the tasks it depends on (?)

Is there a way to call a task as if it were a method? Or how can I achieve anything similar?

Source: (StackOverflow)

What's the difference between equal?, eql?, ===, and ==?

I am trying to understand the difference between these four methods. I know by default that == calls the method equal? which returns true when both operands refer to exactly the same object.

=== by default also calls == which calls equal?... okay, so if all these three methods are not overridden, then I guess ===, == and equal? do exactly the same thing?

Now comes eql?. What does this do (by default)? Does it make a call to the operand's hash/id?

Why does Ruby have so many equality signs? Are they supposed to differ in semantics?

Source: (StackOverflow)

A concise explanation of nil v. empty v. blank in Ruby on Rails

I find myself repeatedly looking for a clear definition of the differences of nil?, blank?, and empty? in Ruby on Rails. Here's the closest I've come:

  • blank? objects are false, empty, or a whitespace string. For example, "", " ", nil, [], and {} are blank.

  • nil? objects are instances of NilClass.

  • empty? objects are class-specific, and the definition varies from class to class. A string is empty if it has no characters, and an array is empty if it contains no items.

Is there anything missing, or a tighter comparison that can be made?

Source: (StackOverflow)

When to use rspec let()?

I tend to use before blocks and set instance variables in them and then use them across my examples, but recently I came upon let(). According to rspec docs, it is used to

... to define a memoized helper method. The value will be cached across multiple calls in the same example but not across examples.

My question is how is this different from using instance variables in before blocks? And also when should you use let() vs before()?

Source: (StackOverflow)

How to install a specific version of a ruby gem?

Using the command-line gem tool, how can I install a specific version of a gem?

Source: (StackOverflow)

Rails I18n validation deprecation warning

I just updated to rails 4.0.2 and I'm getting this warning:

[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.

Is there any security issue in setting it to false?

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)

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

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

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 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)

How can I write a switch statement in Ruby?

How do I write a switch statement in Ruby?

Source: (StackOverflow)