Ruby Overview

Strings

Array Exercises

  • An Array uses the familiar [a, b, c] format.
  • Are sized dynamically and can be of mixed types.  a = [‘string’, 1, :symbol]

Try typing this in IRB

a = [1, 2, 3]

a << 4 a[0]

a.first a.last

What do you think will happen?

“Chelsea will beat Liverpool”[0,1]

Hash Exercises

  • A Hash looks like my_hash = {:a_symbol => 3, “a string” => 4}
  • Are referenced like my_hash[:a_symbol] #=> 3
  • like an associative map  

Try typing this in IRB

currencies = {‘RWF’ => ‘Rwanda Franc’,
‘USD’ => ‘United States Dollar’}

currencies['RWF'] currencies['USD'] currencies['ABC']

Symbol Exercises

  • A symbol looks like :this_is_a_symbol
  • starts with a colon :
  • can be used in similar places as a string but tells the interpreter and programmers that it means “the thing named :this_is_a_symbol” rather than text
  • In ruby, we prefer symbols over hardcoded globals or strings. They’re very lightweight.

Try typing this in IRB

:rwanda

:ruby_on_rails

Additional Exercises