Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Today we covered some different ways on how to write less code with hashes in a class.
- class Phone
- attr_reader :brand, :color, :carrier
- attr_writer :carrier
- def initialize (input_hash)
- @brand = input_hash[:brand]
- @color = input_hash[:color]
- @carrier = input_hash[:carrier]
- end
- def print_info
- "I have a #{color} #{brand} with #{carrier}"
- end
- end
- phone1 = Phone.new({:brand => "iphone", :color => "black", :carrier => "Verizon"})
- phone2 = Phone.new({brand: "Nexus 5x", color: "black", carrier: "Project Fi"})
- p phone1.print_info
- p phone2.print_info
Add Comment
Please, Sign In to add comment