Week 04 Ruby Part 2 - Code-the-Dream-School/rails-guidebook GitHub Wiki
Week | Topic | Learning Objectives | Key Resources |
---|---|---|---|
4 | Ruby Part 2 |
|
Lesson Materials Coding Assignment |
-
Introduction to Blocks: Blocks in Ruby are similar to callbacks in JavaScript, with specific syntax (
do...end
or{...}
) and block arguments. -
Numbered Block Arguments: Use
_1
,_2
, etc., to refer to block arguments. -
Methods with Blocks: Methods like
each
,map
, andreduce
can take blocks.
-
Method Declaration: Use
def
to declare methods. The last expression's value is returned. -
Guard Clauses: Use
return
for early exits. - Variable Scope: Variables inside methods are not accessible outside.
-
Class Basics: Classes are declared with
class
and have instance variables and initializer methods. -
Creating Instances: Use
new
to create instances. -
Instance Methods and Accessors: Use
attr_accessor
for getters and setters or define them explicitly. -
Class Methods and Variables: Use
self
for class methods and@@
for class variables. - Inheritance: Classes can inherit from other classes.
- Introduction to Modules: Modules are used for code reuse and can be included in classes.
-
Requiring Files: Use
require
to include other Ruby files, enabling code organization.
- Standard Library: Utilize Ruby's standard library, documented at ruby-doc.org.
- Ruby Gems: Install and use gems from RubyGems.org for reusable libraries.
#1. divisible.rb
def divisible
(1..100).select { |n| n % 2 == 0 || n % 3 == 0 || n % 5 == 0 }
end
puts divisible
#2. hangman.rb
def hangman(word, letters)
word.chars.map { |char| letters.include?(char) ? char : '_' }.join
end
puts hangman("bob", ["b"]) # => "b_b"
puts hangman("alphabet", ["a", "h"]) # => "a__ha___"
#3. hash_to_array.rb
def hash_to_array(hash)
keys = hash.keys
values = hash.values
[keys, values]
end
#Collecting user input
hash = {}
5.times do
print "Enter a key: "
key = gets.chomp
print "Enter a value: "
value = gets.chomp
hash[key] = value
end
keys, values = hash_to_array(hash)
puts "Keys: #{keys}"
puts "Values: #{values}"
#4. sums.rb
class Sum1
attr_accessor :total
def initialize(a, b)
@total = a + b
end
end
class Sum2
def initialize(a, b)
@a = a
@b = b
end
def new_total
@a + @b
end
end
sum1 = Sum1.new(5, 6)
sum2 = Sum2.new(5, 6)
puts sum1.total # => 11
puts sum2.new_total # => 11
#5. sort_blocks.rb
class Book
attr_reader :author, :title, :count
def initialize(author, title, count)
@author = author
@title = title
@count = count
end
def to_s
"author: #{author} | title: #{title} | count: #{count}"
end
end
book_array = [
Book.new("Beatrice Potter", "Peter Rabbit", 25),
Book.new("Henry Fielding", "Tom Jones", 12),
Book.new("Bob Woodward", "All the President's Men", 30)
]
#Sorting by author
sorted_by_author = book_array.sort { |a, b| a.author.downcase <=> b.author.downcase }
puts "Sorting alphabetically by author"
puts sorted_by_author
#Sorting by title
sorted_by_title = book_array.sort { |a, b| a.title.downcase <=> b.title.downcase }
puts "Sorting alphabetically by title"
puts sorted_by_title
#Sorting by count
sorted_by_count = book_array.sort { |a, b| a.count <=> b.count }
puts "Sorting by number of copies"
puts sorted_by_count
#6. block_method.rb
def do_calc
yield(7, 9)
end
puts do_calc { |a, b| a + b } # => 16
puts do_calc { |a, b| a * b } # => 63
#7. chuck_norris_facts.rb
require 'faker'
loop do
print "Do you want to know a fact about Chuck Norris? (yes/no): "
answer = gets.chomp.downcase
break if answer != 'yes'
puts Faker::ChuckNorris.fact
end