EzDevInfo.com

ruby interview questions

Top ruby frequently asked interview questions

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 can I write a switch statement in Ruby?

How do I write a switch statement in Ruby?


Source: (StackOverflow)

Advertisements

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)

What does map(&:name) mean in Ruby?

I found this code in a RailsCast:

def tag_names
  @tag_names || tags.map(&:name).join(' ')
end

What does the (&:name) in map(&:name) mean?


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)

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)

Why use Ruby's attr_accessor, attr_reader and attr_writer?

Ruby has this handy and convenient way to share instance variables by using keys like

attr_accessor :var
attr_reader :var
attr_writer :var

Why would I choose attr_reader or attr_writer if I could simply use attr_accessor? Is there something like performance (which I doubt)? I guess there is a reason, otherwise they wouldn't have made such keys.


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)

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)

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)

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)

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)

Error installing mysql2: Failed to build gem native extension

I am having some problems when trying to install mysql2 gem for Rails. When I try to install it by running bundle install or gem install mysql2 it gives me the following error:

Error installing mysql2: ERROR: Failed to build gem native extension.

How can I fix this and successfully install mysql2?


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)

Check if a value exists in an array in Ruby

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)