color end end cat = Cat.new(breed: 'Munchkin cat', color: 'ginger') DATA INPUTS puts "I'm looking for a " + cat # => TypeError: can't convert Cat into String # Implicit
color end end cat = Cat.new(breed: 'Munchkin cat', color: 'ginger') DATA INPUTS puts "I'm looking for a “ + cat.to_s # => I have a #<Cat:0x007f81a40ce1d0> # Explicit puts "I'm looking for a " + cat # => TypeError: can't convert Cat into String # Implicit
color end end cat = Cat.new(breed: 'Munchkin cat', color: 'ginger') DATA INPUTS puts "I'm looking for a “ + cat.to_s # => I have a #<Cat:0x007f81a40ce1d0> # Explicit puts "I'm looking for a " + cat # => TypeError: can't convert Cat into String # Implicit # String Interpolation puts "I'm looking for a #{cat}” # => I have a #<Cat:0x007f81a40ce1d0>
DuckRecording def quack play end def play 'Quack!' end end def make_it_quack(duck) duck.quack end # => “Quack!” puts make_it_quack(Duck.new) # => “Quack!” puts make_it_quack(DuckRecording.new)
moo 'Moooo' end end class DuckRecording def quack play end def play 'Quack!' end end DATA INPUTS def make_it_quack(animal) animal.moo if animal.is_a?(Cow) end def make_it_quack(animal) raise TypeError unless animal.is_a?(Duck) end
moo 'Moooo' end end class DuckRecording def quack play end def play 'Quack!' end end DATA INPUTS def make_it_quack(animal) animal.moo if animal.is_a?(Cow) end def make_it_quack(animal) raise TypeError unless animal.is_a?(Duck) end
soundoff quack end end DATA INPUTS def make_it_quack(animal) animal.quack if animal.respond_to?(quack) end #Solution #1: def make_it_quack(animal) animal.soundoff end #Solution #2:
function for strict requirements class Bitcoin def initialize(value) @value = value end def to_peso Peso(@value) * 1.5 end end def Peso(input) raise TypeError if input.is_a?(String) Float(input) end
do something end rescue # error when birthdate is nil end end def age if birthdate.present? # do something else 0 end end DATA INPUTS class Person def initialize(name, birthdate) @name = name @birthdate = birthdate end end
do something end rescue # error when birthdate is nil end end def age if birthdate.present? # do something else 0 end end DATA INPUTS class Person def initialize(name, birthdate) @name = name @birthdate = birthdate end end
self.birthdate = birthdate end def birthdate=(new_birthdate) raise TypeError, "Invalid birth date" unless new_birthdate.is_a?(Date) @birthdate = new_birthdate end end DATA INPUTS
from (irb):114:in `birthdate=' from (irb):110:in `initialize' from (irb):134:in `new' from (irb):134 from /bin/irb:11:in `<main>' person = Person.new(“Enzo”, Date.new(2011,03,26)) => #<Person:0x007f980901b190 @name="Enzo", @birthdate=#<Date: 2011-03-26 ((2455647j,0s,0n), +0s,2299161j)>>
exists, raise a KeyError if it doesn’t • fetch(key_name, default_value): get the value if the key exists, return default_value otherwise • fetch(key_name) { |key| "default" }: get the value if the key exists, otherwise run the supplied block and return the value.
h.fetch(:foobar, “Hello world!” => "Hello World!" h.fetch(:foobar) KeyError: key not found: :foobar from (irb):159:in `fetch' from (irb):159 from /bin/irb:11:in `<main>'
"Anonymous" end def authenticated? false end end def current_user if session[:user_id] User.find(session[:user_id]) else GuestUser.new(session) end end DATA INPUTS
"Anonymous" end def authenticated? false end end def current_user if session[:user_id] User.find(session[:user_id]) else GuestUser.new(session) end end DATA INPUTS def greeting "Hello, #{current_user.name}" end if current_user.authenticated? render_logout_button else render_login_button end
INPUTS class Person def nickname nick end end class Person def nickname nick || first_name end end def render_details(person) content_tag(:h1, person.name) + \ content_tag(:h2, person.nickname) end
Triangle def initialize(pointX, pointY, pointZ) end end point = Struct.new(:x, :y) class Plot def draw_line(Line.new(pointX, pointY)) end def draw_triangle(Triangle.new(pointX, pointY, pointZ)) end end
Products", "url": "/blog/ten-great-new-products/", "body": "<p>Here's ten new products that make great gifts...</ p>", "summary": "Here's ten new products that make great gifts...", "author": "Clary Johnson" }, { "id": 3, "title": "The Laughing Monsters: A Novel", "url": "/blog/laughing-monsters-a-novel/", "body": "<p>Denis Johnson's The Laughing Monsters is a high- suspense tale</p>", "summary": "Denis Johnson's The Laughing Monsters is a high- suspense tale", "author": "Denis Johnson" }, ] RETURNING DATA
"blank"]] attempts = 0 candy = nil catch(:found) do floor.each do |row| row.each do |tile| attempts += 1 if tile == "jawbreaker" || tile == "gummy" candy = tile throw(:found) end end end end ERROR HANDLING https://rubymonk.com/learning/books/4-ruby-primer-ascent/ chapters/41-exceptions/lessons/93-throw-and-catch
"blank"]] attempts = 0 candy = catch(:found) do floor.each do |row| row.each do |tile| attempts += 1 throw(:found, tile) if tile == "jawbreaker" || tile == "gummy" end end end ERROR HANDLING https://rubymonk.com/learning/books/4-ruby-primer-ascent/ chapters/41-exceptions/lessons/93-throw-and-catch
e do_rescue_1 end def do_work_2_method do_work2 rescue Exception e do_rescue_2 end Solution #1: ERROR HANDLING Solution #2: def your_buggy_method do_work1 rescue Exception e do_rescue_1 do_work2 rescue Exception e do_rescue_2 end
Grimm • Exceptional Ruby: Master the Art of Handling Failure in Ruby by Avdi Grimm • Eloquent Ruby by Russ Olsen • Effective Ruby: 48 Specific Ways to Write Better Ruby by Peter J. Jones Further Readings