Skip to content

Instantly share code, notes, and snippets.

@zeroeth
Created February 20, 2014 00:58
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 zeroeth/9104871 to your computer and use it in GitHub Desktop.
Save zeroeth/9104871 to your computer and use it in GitHub Desktop.
ruby lovin
class Mizu
# getter setters for instance variables
attr_accessor :name, :is_coding
attr_accessor :bite_count
attr_accessor :zombie_stats
attr_reader :werewolfness
@@people_bitten_by_vampires = []
def initialize name, is_coding = false
# set properties with getter/setters
self.name = name
self.is_coding = is_coding
self.bite_count = 0
# set property directly
@werewolfness = 99
# hash (also seen as { brain_list: 200, rotting: 5 } in ruby 1.9)
self.zombie_stats = { :brain_lust => 200, :rotting => 5 }
end
# class level method
def self.lycanthropes
return @@people_bitten_by_vampires
end
# instance method
def bite name
puts "#{name} was bitten"
@@people_bitten_by_vampires.push name
self.bite_count += 1
end
end
# create
yuchen = Mizu.new "yuchen"
# set
yuchen.is_coding = false
yuchen.zombie_stats[:brain_lust] = 350
# access
puts yuchen.name
puts yuchen.werewolfness
puts Mizu.lycanthrope
kevin = Mizu.new "zeroeth", true
yuchen.bite "kevin"
kevin.bite "yuchen"
# shared across all instances of class
puts Mizu.lycanthropes.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment