Created
September 24, 2015 16:13
-
-
Save karapetyan/1138d84e0b546b47a3e9 to your computer and use it in GitHub Desktop.
This file contains 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
module Inventoryable | |
module ClassMethods | |
def create(attributes) | |
object = new(attributes) | |
instances << object | |
object | |
end | |
def instances | |
@instances ||= [] ## in this case its instance CLASS variable !? | |
end | |
end | |
def stock_count | |
@stock_count ||= 0 | |
end | |
def stock_count=(number) | |
@stock_count = number | |
end | |
def in_stock? | |
@stock_count > 0 ? true : false | |
end | |
end | |
class Shirt | |
extend Inventoryable::ClassMethods | |
include Inventoryable | |
attr_accessor :attributes | |
def initialize(attributes) | |
@attributes = attributes | |
end | |
def instances | |
puts @instances | |
end | |
end | |
class Pant | |
include Inventoryable | |
attr_accessor :attributes | |
def initialize(attributes) | |
@attributes = attributes | |
@stock_count += 1 | |
end | |
end | |
class Accessory | |
include Inventoryable | |
attr_accessor :attributes | |
def initialize(attributes) | |
@attributes = attributes | |
@stock_count += 1 | |
end | |
end | |
shirt1 = Shirt.create(name: "Vans", size: "L") | |
shirt2 = Shirt.create(name: "Vans", size: "XL") | |
shirt1.stock_count = 40 | |
puts Shirt.instances # each created object stored in instacne class variable? @instances? | |
puts "!!! #{shirt1.instances}" #can't access to instance class variable |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment