Skip to content

Instantly share code, notes, and snippets.

@karapetyan
Created September 24, 2015 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karapetyan/1138d84e0b546b47a3e9 to your computer and use it in GitHub Desktop.
Save karapetyan/1138d84e0b546b47a3e9 to your computer and use it in GitHub Desktop.
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