Ruby is the programming language we use on the backend.
Goals
After finishing this lesson you should be able to read and write simple Ruby programs.
Gain an understanding of the following concepts:
- Working with basic datatypes:
String
,Integer
,Float
,Boolean
,Array
,Hash
- Control flow:
if
,each
,case
,break
... - Functions:
def
,return
, implicit return in methods and blocks - Errors,
raise
andrescue
- Classes and inheritance
- The difference between class methods and instance methods (
def self.method
vs.def method
) - Modules and
include
- Code blocks ("procs", "lambdas")
- Input and output
- Simple regular expressions
Resources
Tutorials
Here are some tutorial to get you started with learning Ruby. You don't need to read them all. Pick some that work for you.
- codeacademy.com Show archive.org snapshot : Interactive tutorial
- Introduction to Programming with Ruby Show archive.org snapshot : Alternative tutorial
- Ruby from other languages Show archive.org snapshot : Outlines the differences if you're coming from C/C++, Java, Perl, PHP or Python.
- RubyMonk: Ruby Primer Show archive.org snapshot : Great examples for many common Ruby idioms
- Understanding the Ruby Exception Hierarchy Show archive.org snapshot
-
I Love Ruby: Get started with the greatest programming language made for humans.
Show archive.org snapshot
- Note that chapters 16 (RDoc), 23 (Multi Threading) and everything after chapter 27 is irrelevant for now
References
When you're looking for detailed information on a Ruby class or method, you can Google it or use one of the documentation references listed here Show archive.org snapshot
Exercises
Create a separate directory for each exercise.
Counting words
Write a small ruby programm count_words.rb
that accepts a filename, counts the number of words, lines and paragraphs, and outputs the result.
For example:
$ ruby count_words.rb test.txt
test.txt has 123 words
test.txt has 13 lines
test.txt has 4 paragraphs
Hint
- Find out about
ARGV
.- Look up
File
in one of the references above.- You can create random text on randomtextgenerator.com Show archive.org snapshot .
- Learn about regular expressions Show archive.org snapshot .
Address search
Write a Contact
class that models an address book entry.
It should offer an API like this:
contact = Contact.new(first_name: 'Anna', last_name: 'Muster', street: 'Foo Avenue 77')
contact.first_name # => 'Anna'
contact.last_name # => 'Muster'
A Contact
object should be able to store:
- First name
- Last name
- Street
- Postal code
- City
- Phone numbers
All fields are optional, except for #last_name
. If we try to instantiate a contact without a last name, the constructor raises an error:
Contact.new(first_name: 'Anna') # raises ArgumentError
Now build an AddressBook
class that can store a list of contacts in memory:
addresses = AddressBook.new
addresses.add Contact.new(first_name: 'Frederik', last_name: 'Foo')
addresses.add Contact.new(first_name: 'Berta', last_name: 'Beispiel', phone: '556677')
addresses.add Contact.new(first_name: 'Anna', last_name: 'Muster', street: 'Foo Avenue 77')
addresses.size # => 3
Now write a method AddressBook#search
that takes a query string and returns an array of Contact
objects that match the given word in any of their properties (name, street, city, etc.):
results = addresses.search('foo') # returns an Array of "Frederik" and "Anna" contacts
results.size # => 2
results[0].class # => Contact
results[0].first_name # => "Frederik"
results[1].street # => "Foo Avenue 77"
Hint
You can convert any object to a string by calling
#to_s
on it.
The matching should be case-insensitive Show archive.org snapshot .
Also when the query string contains more than one word, it returns contacts that match all of the words in any property:
results = addresses.search('77 berta')
results.size # => 1
results[0].first_name # => "Berta"
Errors
Change the AddressBook
class so the #add
method throws a DuplicateContact
error when the user tries to add a contact that already exist. We consider two contacts to be duplicates if they have the same first and last name.
Hint
Create a custom error class Show archive.org snapshot that inherits from
StandardError
.
Blocks and monkey patches
Give Array
a new method #random_each
. The method should iterate through the array elements in random order and call the given block for each iteration.
For example, the following should work:
[1, 2, 3, 4, 5].random_each do |value|
puts value * -1
end
And get an output like this:
-4
-2
-1
-5
-3
Hint
- Research "monkey patching" Show archive.org snapshot .
- Research Ruby's
yield
keyword