How to write a switch statement in Ruby

Ruby uses the case expression instead. Ruby compares the object in the when clause with the object in the case clause using the === operator. For example, 1..5 === x, and not x === 1..5. This allows for sophisticated when clauses as seen above. Ranges, classes and all sorts of things can be tested for rather than just equality. Unlike switch statements in many other languages, Ruby’s case does not have fall-through, so … Read more

What does the “map” method do in Ruby?

The map method takes an enumerable object and a block, and runs the block for each element, outputting each returned value from the block (the original object is unchanged unless you use map!): Array and Range are enumerable types. map with a block returns an Array. map! mutates the original array. Where is this helpful, and what is the difference between map! and each? Here is an example: … Read more

What is the meaning of *nix?

*nix just means operating systems that are like the old workhorse Unix. Some examples include Linux, FreeBSD, and Mac OS X (its kernel, Darwin, is based on BSD). The main relation between *nix and Ruby is just a pragmatic one; most Ruby developers seem to prefer to work on Unix-like OSes (typically Linux or Mac … Read more