Bitcoin maximalist.

Thoughts & Technical Writings.

Under the Hood of Ruby’s Interpreter: A Straightforward Example

| Comments

Flatiron, Day 4:

First, some things Avi said today:

Iteration is about going over a collection of objects. Enumeration is the same thing but you’re given the objects.
object.method… the dot is called dot notation. It’s how we send messages to objects.
.. (read: ‘dot dot’) is a literal constructor for Range.
You need to keep “rf -rf /” handy. You know, in case of those Terminator situations.
Comptuer languages fall into 2 categories: those designed for human problems, and those designed for computer problems. JavaScript was designed to be read quickly by web browsers (i.e. a computer problem).
Computers will get faster, but people won’t get any smarter.

Now, on to the post subject… let’s walk stepwise through a (very) simple program in Ruby:

Step 0:

x = 1
if x == 1
    puts "x is equal to 1!"

Step 1:

if 1 == 1
    puts "x is equal to 1!"

Step 2:

if true
    puts "x is equal to 1!"

Step 3:

puts "x is equal to 1!"  #=> "x is equal to 1!"

FizzBuzz ultimate solution:

def fizzbuzz (i)
    [("fizz" if i % 3 == 0), ("buzz" if i % 5 == 0)].join

case vs. if

if x == 1
    puts "x wins"
elsif o == 1
    puts "o wins"

case object # only can check if condition applies to one object

when condition # condition cannot be a logical operand; must use if/end if you want a unique logic applied for each “case”