SHARE
TWEET

Untitled

a guest Jun 24th, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # File: cars-8.rb
  2. #
  3. class Car
  4.   attr_accessor :make, :model, :color
  5. end
  6.  
  7. def print_values(values, column_size)
  8.   puts values.
  9.       map {|column_header| column_header.ljust(column_size)}.
  10.       join('|')
  11. end
  12.  
  13. def print_car_data(cars)
  14.   column_size = 30
  15.   number_of_columns = 3
  16.  
  17.   print_values ['Make', 'Model', 'Color'], column_size
  18.  
  19.   puts '=' * (column_size * number_of_columns + number_of_columns - 1)
  20.  
  21.   cars.each do |car|
  22.     print_values ["#{car.make}", "#{car.model}", "#{car.color}"], column_size
  23.   end
  24. end
  25.  
  26. cars = [Car.new, Car.new]
  27.  
  28. cars.each do |car|
  29.   puts "Car: Make: #{car.make}, Model: #{car.model}, Color: #{car.color}"
  30. end
  31.  
  32. # Give the cars some values
  33.  
  34. [['Aston Martin', 'Cygnet', 'Red'],
  35.  ['Aston Martin', 'Rapide', 'Blue']].
  36.  each_with_index do |attributes, index|
  37.   cars[index].make = attributes[0]
  38.   cars[index].model = attributes[1]
  39.   cars[index].color = attributes[2]
  40. end
  41.  
  42. print_car_data cars
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top