Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. # File: open-struct-1.rb
  2. #
  3. require 'ostruct'
  4.  
  5. def print_values(values, column_size)
  6. puts values.
  7. map {|column_header| column_header.ljust(column_size)}.
  8. join('|')
  9. end
  10.  
  11. def print_car_data(cars)
  12. column_size = 30
  13. number_of_columns = 3
  14.  
  15. print_values ['Make', 'Model', 'Color'], column_size
  16.  
  17. puts '=' * (column_size * number_of_columns + number_of_columns - 1)
  18.  
  19. cars.each do |car|
  20. print_values ["#{car.make}", "#{car.model}", "#{car.color}"], column_size
  21. end
  22. end
  23.  
  24. cars = [OpenStruct.new, OpenStruct.new]
  25.  
  26. cars.each do |car|
  27. puts "Car: Make: #{car.make}, Model: #{car.model}, Color: #{car.color}"
  28. end
  29.  
  30. # Give the cars some values
  31.  
  32. [['Aston Martin', 'Cygnet', 'Red'],
  33. ['Aston Martin', 'Rapide', 'Blue']].
  34. each_with_index do |attributes, index|
  35. cars[index].make = attributes[0]
  36. cars[index].model = attributes[1]
  37. cars[index].color = attributes[2]
  38. end
  39.  
  40. print_car_data cars
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement