Guest User

Untitled

a guest
Nov 23rd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. Today we covered some different ways on how to write less code with hashes in a class.
  2.  
  3. class Phone
  4.  
  5. attr_reader :brand, :color, :carrier
  6. attr_writer :carrier
  7.  
  8. def initialize (input_hash)
  9. @brand = input_hash[:brand]
  10. @color = input_hash[:color]
  11. @carrier = input_hash[:carrier]
  12. end
  13.  
  14. def print_info
  15. "I have a #{color} #{brand} with #{carrier}"
  16. end
  17.  
  18. end
  19.  
  20. phone1 = Phone.new({:brand => "iphone", :color => "black", :carrier => "Verizon"})
  21. phone2 = Phone.new({brand: "Nexus 5x", color: "black", carrier: "Project Fi"})
  22.  
  23. p phone1.print_info
  24. p phone2.print_info
Add Comment
Please, Sign In to add comment