An Introduction to the Ruby Language
Reading
Ruby files
- Store our code in files that end in
.rb
- Run our code from the terminal via
ruby filenamehere.rb
- We can access an interactive console with the program
irb
Variables and basic types
Strings
name = "Gavin"
name.length
- String interpolation
new_string = "My score is #{41 + 1}"
Numbers
score = 42
Arrays
scores = [100, 98, 42, 65]
Indexing
scores[0]
is the first thing in the array (100)scores[1]
is the second thing in the array (98)
Negative indexing
scores[-1]
is the last thing in the array (65)scores[-2]
is the second to last thing in the array (42)
Returns
nil
if the index isn't therescores[500]
isnil
Methods
length
gives you the length of the array
Can store mixed types
my_array = ["Gavin", 42, "Toni", 100]
array_with_arrays_inside = ["Gavin", 42, ["Toni", "Jason"], 100]
Hash (like a dictionary)
- Declaring
person = { "name" => "Gavin Stark", "score" => 42, "favorite_color" => "blue" }
- Accessing
person["score"]
(returns 42)person["favorite_color"]
(returns blue)
- Missing keys
- Returns
nil
if the key isn't thereperson["address"]
returnsnil
- Returns
- Values (and keys, though they are usually strings) can be complex
person = { "name" => "Gavin Stark", "scores" => [100, 98, 42, 64] }
person["scores"]
is[100, 98, 42, 64]
person["scores"][2]
is42
sinceperson["scores"]
is an array, and then we index the array at2
to get the third element
Interacting with users
Printing values
puts
outputs information without any formatting. Good for user outputp
outputs information formatted for programmer friendliness. Good for debugging.
name = "Gavin"puts name # Would output: Gavinp name # Would output: "Gavin"empty_string = ""puts empty_string # Would output:p empty_string # Would output: ""
Reading values
gets
retrieves information from input (usually the terminal from the user typing)- However, it includes a
newline
character (the return key that ends a line) - We use
chomp
to remove it.
puts "What is your name?"name_with_newline = getsname = name_with_newline.chomp# orputs "What is your name?"name = gets.chomp# orputs "What is your name?"name = gets(chomp: true)
Control Flow
- if statements
if name == "Gavin"puts "The name was Gavin!"elseputs "The name isn't Gavin"end
Looping
- loop / break
loop doputs "Give me some input"input = gets.chompif input == "done"breakendputs "You said #{input}"end
Ruby Comments
- Comments start with a
#
mark. With a bare#
, anything that follows will be ignored by Ruby.
Methods
- Organizing code
- Placing a name on a set of steps or a way of doing something
- Methods have a name, and optionally a set of inputs (arguments) and a return
- Example:
# This method has no inputs or returndef say_helloputs "Hello there!"end# This method has an input but no returnsdef say_hello(name)puts "Hello there #{name}!"end# This method has inputs and a returndef make_sentence(name, score)return "The score for #{name} is #{score}"end
- Without a
return
keyword, a method returns the value of the last statement of the function.
# This method has inputs and a returndef make_sentence(name, score)"The score for #{name} is #{score}"end
More control flow
Conditionals
- Are two things true (AND):
if score > 10 && score < 80
- Are either of two things true (OR):
if score > 90 || name == "Gavin"
- Are two things true (AND):
What if we are looking for a few options? For this, we have the
case
statementcase color_namewhen "red"puts "#F00"when "green"puts "#0F0"when "blue"puts "#00F"when "black"puts "#000"when "white"puts "#FFF"end