Determining type of an object in ruby

The proper way to determine the “type” of an object, which is a wobbly term in the Ruby world, is to call object.class. Since classes can inherit from other classes, if you want to determine if an object is “of a particular type” you might call object.is_a?(ClassName) to see if object is of type ClassName … Read more

How to break out from a ruby block?

Use the keyword next. If you do not want to continue to the next item, use break. When next is used within a block, it causes the block to exit immediately, returning control to the iterator method, which may then begin a new iteration by invoking the block again: f.each do |line| # Iterate over … Read more

How to update Ruby Version 2.0.0 to the latest version in Mac OSX Yosemite?

Open your terminal and run curl -sSL https://raw.githubusercontent.com/rvm/rvm/master/binscripts/rvm-installer | bash -s stable When this is complete, you need to restart your terminal for the rvm command to work. Now, run rvm list known This shows the list of versions of the ruby. Now, run rvm install ruby@latest to get the latest ruby version. If you … Read more

Difference between a class and a module

╔═══════════════╦═══════════════════════════╦═════════════════════════════════╗ ║ ║ class ║ module ║ ╠═══════════════╬═══════════════════════════╬═════════════════════════════════╣ ║ instantiation ║ can be instantiated ║ can *not* be instantiated ║ ╟───────────────╫───────────────────────────╫─────────────────────────────────╢ ║ usage ║ object creation ║ mixin facility. provide ║ ║ ║ ║ a namespace. ║ ╟───────────────╫───────────────────────────╫─────────────────────────────────╢ ║ superclass ║ module ║ object ║ ╟───────────────╫───────────────────────────╫─────────────────────────────────╢ ║ methods ║ class methods and ║ module methods … Read more

How to find a min/max with Ruby

You can do [5, 10].min or [4, 7].max They come from the Enumerable module, so anything that includes Enumerable will have those methods available. v2.4 introduces own Array#min and Array#max, which are way faster than Enumerable’s methods because they skip calling #each. @nicholasklick mentions another option, Enumerable#minmax, but this time returning an array of [min, … Read more

What is the difference between include and extend in Ruby?

extend – adds the specified module’s methods and constants to the target’s metaclass (i.e. the singleton class) e.g. if you call Klazz.extend(Mod), now Klazz has Mod’s methods (as class methods) if you call obj.extend(Mod), now obj has Mod’s methods (as instance methods), but no other instance of of obj.class has those methods added. extend is … Read more

Tell Ruby Program to Wait some amount of time

Like this: sleep(num_secs) The num_secs value can be an integer or float. Also, if you’re writing this within a Rails app, or have included the ActiveSupport library in your project, you can construct longer intervals using the following convenience syntax: sleep(4.minutes) # or, even longer… sleep(2.hours); sleep(3.days) # etc., etc. # or shorter sleep(0.5) # … Read more

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

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