Skip to content

Latest commit

 

History

History
40 lines (32 loc) · 1.46 KB

File metadata and controls

40 lines (32 loc) · 1.46 KB
lang EN
title All's Well That Ends Well
answer All's Well That Ends Well
ok true
error Use 1605 as the second parameter
load prev

Okay, we got some data from the internet, we selected what we wanted and printed it out. What is left to improve? We could print the results a bit prettier. Like add the year and align titles and years.

This means printing several values on one line. Ruby has a neat way of doing that. It is just like printing a string like: puts "Hi, my name is Jimmy". But instead of the literal value Jimmy we use the value of a variable.

First replace Jimmy with #{}. If Ruby sees a hash symbol # followed by a curly brace { it looks for a variable between the first brace and the following closing brace }. So we can use this: "Hi, my name is #{name}".

Let's change our code a bit

def print_plays(year_from, year_to)
  get_shakey["William Shakespeare"]
    .select { |k, v|
      year_from <= v["finished"] &&
      year_to   >= v["finished"]
    }.each { |k, v|
      puts "#{v["title"].ljust(30)} #{v["finished"]}"
    }
end
print_plays(1600, 1605)

I have added .ljust(30) to the title. This way the title is left justified with a minimum length of 30 characters so the years align nicely.

See if you can change the ouput of the program so that it shows the plays like this: 1600 -> As You Like It

All's Well That Ends Well? Hey we're not done yet, but the end is in sight!