You are on page 1of 2

Data Types

Numbers
Booleans (true or false)
Strings (words or phrases)

The print command just takes whatever you give it and prints it to the
screen. puts (for "put string") is slightly different: it adds a new (blank) line after the
thing you want it to print. You use them like this:
puts "What's up?"
print "Oxnard Montalvo"

Valentim.Length

Remember, you call a method by using the . operator, like this: "string".method.
1.

First we introduce one new method, capitalize, here. It capitalizes the first
letter of a string and makes the rest of the letters lower case. We assign the result
to answer2
<
(x && (y || w)) && z

hash is a collection of key-value pairs. Hash syntax looks like this:


hash = {
key1 => value1,
key2 => value2,
key3 => value3
}

Adding to a Hash

We can add to a hash two ways: if we created it using literal notation, we can simply
add a new key-value pair directly between the curly braces. If we used Hash.new,
we can add to the hash using bracket notation:
pets = Hash.new
pets["Stevie"] = "cat"
# Adds the key "Stevie" with the
# value "cat" to the hash

Thankfully, Ruby provides us with a concise alternative: the case statement. The
syntax looks like this:
case language
when "JS"
puts "Websites!"
when "Python"
puts "Science!"
when "Ruby"
puts "Web apps!"
else
puts "I don't know!"
end

Your if and unless statements should be of the form:


action if boolean
action unless boolean

Remember, you don't need an endwith a single-line if or unless!

Quick Review

All this talk of blocks, procs, and lambdas might have your head spinning. Let's take
a minute to clarify exactly what each one is:
1.
2.
3.

A block is just a bit of code between do..end or {}. It's not an object on its
own, but it can be passed to methods like .each or .select.
A proc is a saved block we can use over and over.
A lambda is just like a proc, only it cares about the number of arguments it
gets and it returns to its calling method rather than returning immediately.
There are obviously lots of cases in which blocks, procs, and lambdas can do
similar work, but the exact circumstances of your program will help you decide
which one you want to use.

You might also like