Examples from class: Intro to Ruby

To try the examples below, use the irb command in your terminal to type Ruby interactively and see results!

irb is an example of a “REPL,” a “Read-Execute-Print Loop” that prints the result of each expression you type. Languages that are dynamically typed and interpreted often have a REPL. (Compiled and statically typed languages can too, but they take much greater effort to build and are often limited.)

# Try copying and pasting the examples below into irb, one chunk
# at a time. For example, ask the REPL to do some arithmetic:

1 + 1
3 + 5 * 7

# In Ruby, the function to print some text is called `puts`:

puts "Hello!"

# You can also use `p` to “inspect” a value, which will give you
# more informaiton about the type and internal structure of value
# it is by (for example) putting quotation marks around a string,
# or brackets around an array:

p "Hello!"

# In Ruby, parentheses are optional for function calls! These two
# do the same thing as the two calls above:

puts("Hello!")
p("Hello!")

# Here's a Ruby conditional:

x = 123

if x > 50
  puts "hi"
else
  puts "bye"
end

# A fun thing about Ruby is that (almost) _all_ AST subtrees are
# expressions. You can do this:

y =
  if x > 50
    "hi"
  else
    "bye"
  end
  
puts "The value of y is: #{y}"

# The entire conditional is an expression! This is the same:

y = (
  if x > 50
    "hi"
  else
    "bye"
  end
)

# But wait! What if there are multiple lines in one branch
# of the conditional?

y =
  if x > 50
    "hi"
    347
    "whatever"
  else
    "bye"
  end

p y

# _Everything_ is an expression. A block of multiple lines is an
# expression that evaluates to the last line.
#
# What about this?

y =
  if x > 50
    "hi"
    347
    if x < 100
      "whatever"
    else
      "okay"
    end
  else
    "bye"
  end
  
p y

# See if you can figure out why it sets y to what it does.

# But what if a condition doesn't match, and there's no else clause?

y =
  if x > 1000
   "hi"
  end

p y

# Why does it print that? What will this print?

y =
  if x > 50
    "hi"
    347
    if x < 100
      "whatever"
    end
  else
    "bye"
  end
  
p y

# A teaser for things to come: in Ruby, _every_ value is an object, so every value
# has methods.

"hello".size
[3, 1, 5].size

# Numbers have methods! Take that, Java!

-3.abs
3.times { puts "hi!" }  # wat?!

# You can ask an object what class it is:

"hello".class
[3, 1, 5].class
3.class

# Every value is an object, and every action is a method call. This:

1 + 2

# ...is secretly just a shortcut for calling the + method of the Integer class:

1.+(2)   # wat?!??

# And remember from last week: there are no vtables or itables here; all these
# objects and classes are just hash tables...and you can **alter those hash
# tables while the program is running**. That means that we can -- oh no! --
# redefine the + method of the Integer class:

class Integer
  def +(y)
    self * y
  end
end

puts(3 + 7)  # WAT.

# This is...not something you should do on a practical Ruby project. Believe
# it or not, however, this flexibility opens up some exciting possibilities.
# We'll be delving into this wild new world in the coming days of class.