Skip to main content
Filter by
Sorted by
Tagged with
2308 votes
29 answers
1.1m views

How to write a switch statement in Ruby

How do I write a switch statement in Ruby?
readonly's user avatar
  • 352k
1535 votes
28 answers
1.2m views

How to 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?
user211662's user avatar
  • 18.4k
1266 votes
22 answers
652k views

How to call shell commands from Ruby

How do I call shell commands from inside of a Ruby program? How do I then get output from these commands back into Ruby?
CodingWithoutComments's user avatar
1236 votes
11 answers
822k views

How to convert a string to lower or upper case in Ruby

How do I take a string and convert it to lower or upper case in Ruby?
Heat Miser's user avatar
  • 19.7k
1219 votes
22 answers
450k views

How to pass command line arguments to a rake task

I have a rake task that needs to insert a value into multiple databases. I'd like to pass this value into the rake task from the command line, or from another rake task. How can I do this?
Tilendor's user avatar
  • 48.8k
1216 votes
15 answers
337k views

How to understand nil vs. empty vs. blank in Ruby

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 ...
Arrel's user avatar
  • 13.6k
1158 votes
20 answers
553k views

What is attr_accessor in Ruby?

I am having a hard time understanding attr_accessor in Ruby. Can someone explain this to me?
dennismonsewicz's user avatar
1128 votes
33 answers
575k views

How do I get the current absolute URL in Ruby on Rails?

How can I get the current absolute URL in my Ruby on Rails view? The request.request_uri only returns the relative URL.
Jakub Arnold's user avatar
  • 86.8k
1095 votes
13 answers
282k views

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 ...
Ricardo Acras's user avatar
1063 votes
11 answers
230k views

Understanding the Rails Authenticity Token

What is the Authenticity Token in Rails?
Ricardo Acras's user avatar
1052 votes
8 answers
597k views

Ruby: 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?
mjs's user avatar
  • 64.8k
1013 votes
6 answers
193k views

class << self idiom in Ruby

What does class << self do in Ruby?
randombits's user avatar
  • 48.1k
988 votes
6 answers
356k views

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?
John's user avatar
  • 29.9k
987 votes
8 answers
492k views

What does %w(array) mean?

I'm looking at the documentation for FileUtils. I'm confused by the following line: FileUtils.cp %w(cgi.rb complex.rb date.rb), '/usr/lib/ruby/1.6' What does the %w mean? Can you point me to the ...
Dane O'Connor's user avatar
880 votes
10 answers
1.0m views

How to check whether a string contains a substring in Ruby

I have a string variable with content: varMessage = "hi/thsid/sdfhsjdf/dfjsd/sdjfsdn\n" "/my/name/is/balaji.so\n" "call::myFunction(int const&)\n" ...
BSalunke's user avatar
  • 11.7k
870 votes
10 answers
528k views

Multi-Line Comments in Ruby?

How can I comment multiple lines in Ruby?
Mohit Jain's user avatar
  • 43.8k
854 votes
12 answers
879k views

Execute a command line binary with Node.js

I am in the process of porting a CLI library from Ruby over to Node.js. In my code I execute several third party binaries when necessary. I am not sure how best to accomplish this in Node. Here's an ...
Dave Thompson's user avatar
838 votes
18 answers
580k views

How to get a random number in Ruby

How do I generate a random number between 0 and n?
Mark A. Nicolosi's user avatar
831 votes
46 answers
525k views

How to generate a random string in Ruby

I'm currently generating an 8-character pseudo-random uppercase string for "A" .. "Z": value = ""; 8.times{value << (65 + rand(25)).chr} but it doesn't look clean, and it can't be passed as ...
766 votes
8 answers
608k views

How to check if a specific key is present in a hash or not?

I want to check whether the "user" key is present or not in the session hash. How can I do this? Note that I don't want to check whether the key's value is nil or not. I just want to check whether ...
Mohit Jain's user avatar
  • 43.8k
762 votes
20 answers
411k views

How to "pretty" format JSON output

I would like my JSON output in Ruby on Rails to be "pretty" or nicely formatted. Right now, I call to_json and my JSON is all on one line. At times this can be difficult to see if there is a ...
JP Richardson's user avatar
761 votes
9 answers
321k views

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?
Mark Szymanski's user avatar
738 votes
24 answers
519k views

How to drop columns using Rails migration

What's the syntax for dropping a database table column through a Rails migration?
Ethan's user avatar
  • 9,748
727 votes
6 answers
443k views

Difference between rake db:migrate db:reset and db:schema:load

The difference between rake db:migrate and rake db:reset is pretty clear to me. The thing which I don't understand is how rake db:schema:load is different from the former two. Just to be sure that I ...
Gaurav Agarwal's user avatar
688 votes
18 answers
666k views

How to remove a key from Hash and get the remaining hash in Ruby/Rails?

To add a new pair to Hash I do: {:a => 1, :b => 2}.merge!({:c => 3}) #=> {:a => 1, :b => 2, :c => 3} Is there a similar way to delete a key from Hash ? This works: {:a => ...
Misha Moroshko's user avatar
670 votes
7 answers
726k views

How to write to file in Ruby?

I need to read the data out of database and then save it in a text file. How can I do that in Ruby? Is there any file management system in Ruby?
ohana's user avatar
  • 6,721
668 votes
26 answers
732k views

Ruby function to remove all white spaces?

What is the Ruby function to remove all white spaces? I'm looking for something kind of like PHP's trim()?
gpwu's user avatar
  • 7,037
665 votes
13 answers
196k views

Why are exclamation marks used in Ruby methods?

In Ruby some methods have a question mark (?) that ask a question like include? that ask if the object in question is included, this then returns a true/false. But why do some methods have ...
Lennie's user avatar
  • 10.8k
656 votes
6 answers
232k views

How to match all occurrences of a regular expression in Ruby

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.
Chris Bunch's user avatar
  • 89.3k
655 votes
7 answers
341k views

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", "...
Paul Hoffer's user avatar
  • 12.9k
647 votes
13 answers
275k views

How to remove RVM (Ruby Version Manager) from my system

How can I remove RVM (Ruby Version Manager) from my system?
lucianosousa's user avatar
  • 8,284
643 votes
32 answers
610k views

How to install a gem or update RubyGems if it fails with a permissions error

I'm trying to install a gem using gem install mygem or update RubyGems using gem update --system, and it fails with this error: ERROR: While executing gem ... (Gem::FilePermissionError) You don't ...
Gaurav Taywade's user avatar
631 votes
7 answers
516k views

Begin, Rescue and Ensure in Ruby?

I was wondering if ensure was the Ruby equivalent of finally in C#? Should I have: file = File.open("myFile.txt", "w") begin file << "#{content} \n" rescue # ...
Lloyd Powell's user avatar
  • 18.7k
627 votes
16 answers
559k views

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.
brainfck's user avatar
  • 9,376
614 votes
15 answers
361k views

Checking if a variable is defined?

How can I check whether a variable is defined in Ruby? Is there an isset-type method available?
readonly's user avatar
  • 352k
597 votes
8 answers
231k views

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 ...
denniss's user avatar
  • 17.5k
582 votes
18 answers
545k views

How do you add an array to another array in Ruby and not end up with a multi-dimensional result?

I tried: somearray = ["some", "thing"] anotherarray = ["another", "thing"] somearray.push(anotherarray.flatten!) I expected ["some", "thing"...
ncvncvn's user avatar
  • 5,853
572 votes
5 answers
245k views

Ruby: kind_of? vs. instance_of? vs. is_a?

What is the difference? When should I use which? Why are there so many of them?
Claudiu's user avatar
  • 228k
556 votes
17 answers
157k views

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?
collimarco's user avatar
  • 35.1k
554 votes
1 answer
400k views

How to Uninstall RVM? [duplicate]

Possible Duplicate: How to remove rvm (ruby version manager) from my system? How can I uninstall (or reinstall) RVM on Ubuntu 9.10? I messed up my current installation.
conbask's user avatar
  • 9,971
547 votes
5 answers
336k views

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 ...
Saturn's user avatar
  • 18k
543 votes
9 answers
119k views

Should Gemfile.lock be included in .gitignore?

I'm sort of new to bundler and the files it generates. I have a copy of a git repo from GitHub that is being contributed to by many people so I was surprised to find that bundler created a file that ...
aarona's user avatar
  • 37k
536 votes
33 answers
316k views

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 ...
spacemonkey's user avatar
  • 19.9k
529 votes
15 answers
760k views

How can I update Ruby version 2.0.0 to the latest version in Mac OS X v10.10 (Yosemite)?

I need to update my Ruby version from 2.0.0 to the latest version. I can not use some gems because my version is not updated. I had used Homebrew to install Ruby some time ago. How can I update my ...
ChuchaC's user avatar
  • 5,303
523 votes
11 answers
286k views

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 ...
Owen's user avatar
  • 22.8k
521 votes
13 answers
280k views

How to redirect to a 404 in Rails?

I'd like to 'fake' a 404 page in Rails. In PHP, I would just send a header with the error code as such: header("HTTP/1.0 404 Not Found"); How is that done with Rails?
Yuval Karmi's user avatar
  • 26.6k
520 votes
17 answers
243k views

How to reverse a 'rails generate'

I want to delete all the files it created and roll back any changes made, but not necessarily to the database, but more to the config files. I'd like to automatically delete all the resource mappings ...
marcamillion's user avatar
  • 33.6k
518 votes
19 answers
318k views

Error while installing json gem 'mkmf.rb can't find header files for ruby'

For context, it on a remote server which has a firewall. I'm setting up my environment through a proxy. I have ruby 1.8.7. When I try to gem install.. sudo gem install --http-proxy <host address&...
Navneet's user avatar
  • 9,770
510 votes
25 answers
150k views

Undo scaffolding in Rails

Is there any way to 'undo' the effects of a scaffold command in Rails?
Daniel's user avatar
  • 16.2k
509 votes
16 answers
491k views

Ruby: Can I write multi-line string with no concatenation?

Is there a way to make this look a little better? conn.exec 'select attr1, attr2, attr3, attr4, attr5, attr6, attr7 ' + 'from table1, table2, table3, etc, etc, etc, etc, etc, ' + '...
Zombies's user avatar
  • 25.6k

1
2 3 4 5
4586