When to use RSpec let()?

I always prefer let to an instance variable for a couple of reasons: Instance variables spring into existence when referenced. This means that if you fat finger the spelling of the instance variable, a new one will be created and initialized to nil, which can lead to subtle bugs and false positives. Since let creates … Read more

Is there a “do … while” loop in Ruby?

CAUTION: The begin <code> end while <condition> is rejected by Ruby’s author Matz. Instead he suggests using Kernel#loop, e.g. loop do # some code here break if <condition> end Here’s an email exchange in 23 Nov 2005 where Matz states: |> Don’t use it please. I’m regretting this feature, and I’d like to |> remove … Read more

What is Ruby’s double-colon `::`?

:: is basically a namespace resolution operator. It allows you to access items in modules, or class-level items in classes. For example, say you had this setup: module SomeModule module InnerModule class MyClass CONSTANT = 4 end end end You could access CONSTANT from outside the module as SomeModule::InnerModule::MyClass::CONSTANT. It doesn’t affect instance methods defined … Read more

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

There are pieces to this answer that helped me get what I needed (easy multi-line concatenation WITHOUT extra whitespace), but since none of the actual answers had it, I’m compiling them here: str=”this is a multi-line string”\ ‘ using implicit concatenation’\ ‘ to prevent spare \n\’s’ => “this is a multi-line string using implicit concatenation … Read more

When monkey patching an instance method, can you call the overridden method from the new implementation?

EDIT: It has been 9 years since I originally wrote this answer, and it deserves some cosmetic surgery to keep it current. You can see the last version before the edit here. You can’t call the overwritten method by name or keyword. That’s one of the many reasons why monkey patching should be avoided and … Read more

How to map/collect with index in Ruby?

If you’re using ruby 1.8.7 or 1.9, you can use the fact that iterator methods like each_with_index, when called without a block, return an Enumerator object, which you can call Enumerable methods like map on. So you can do: arr.each_with_index.map { |x,i| [x, i+2] } In 1.8.6 you can do: require ‘enumerator’ arr.enum_for(:each_with_index).map { |x,i| … Read more

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

Modern era update, as stated by mimoralea: In case that you are using ruby 2.0 or 2.2 (thanks @patrick-davey). sudo apt-get install ruby2.0-dev sudo apt-get install ruby2.2-dev sudo apt-get install ruby2.3-dev or, generic way: sudo apt-get install ruby-dev or sudo apt-get install ruby`ruby -e ‘puts RUBY_VERSION[/\d+\.\d+/]’`-dev The first link you’ve posted is exactly your case: … Read more

Undo scaffolding in Rails

First, if you have already run the migrations generated by the scaffold command, you have to perform a rollback first. rake db:rollback You can create scaffolding using: rails generate scaffold MyFoo (or similar), and you can destroy/undo it using rails destroy scaffold MyFoo That will delete all the files created by generate, but not any … Read more

What is the difference between include and require in Ruby?

What’s the difference between “include” and “require” in Ruby? Answer: The include and require methods do very different things. The require method does what include does in most other programming languages: run another file. It also tracks what you’ve required in the past and won’t require the same file twice. To run another file without … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)