Created
February 25, 2019 21:38
-
-
Save CheezItMan/4d829478a0ba4b70c284a9795421cdfb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User | |
attr_reader :email | |
# attr_writer :name | |
attr_accessor :name | |
def initialize(name, email) | |
@name = name | |
@email = email | |
end | |
# def name | |
# return @name | |
# end | |
# def name=(new_name) | |
# @name = new_name | |
# end | |
def email=(new_email) | |
@email = new_email | |
end | |
def puts_self | |
puts self | |
end | |
# def email | |
# return @email | |
# end | |
def summary(name) | |
self.name | |
return "#{self.name}: #{self.email}" | |
end | |
end | |
neo = User.new("Mr. Anderson", "[email protected]") | |
puts neo | |
neo.puts_self | |
hermine = User.new("Hermine Granger", | |
"[email protected]") | |
puts hermine | |
hermine.puts_self | |
# puts "Hello #{neo.name}" | |
# neo.name = "My name is NEO!" | |
# puts neo.summary | |
puts hermine.summary | |
# puts hermine.name | |
# Product Example | |
class Product | |
attr_accessor :name, :quantity_in_stock | |
attr_reader :quantity_sold | |
def initialize(name, quantity_in_stock) | |
@name = name | |
@quantity_in_stock = quantity_in_stock | |
@quantity_sold = 0 | |
end | |
def summary | |
return "#{@name} with: #{@quantity_in_stock} in stock and #{@quantity_sold}" | |
end | |
def available? | |
return quantity_in_stock > 0 | |
end | |
def sell(amount) | |
@quantity_in_stock -= amount | |
@quantity_sold += amount | |
end | |
end | |
# Constants, Classes and local variables. | |
# # Constant | |
# TIP_AMOUNT = 0.15 | |
# ... | |
# TIP_AMOUNT = 0.20 | |
# # Class definition | |
# class Time | |
# # regular variable | |
# total_bill = 37.65 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment