Skip to content

Instantly share code, notes, and snippets.

class Cat
STATES = ["hungry", "sated"].freeze
attr_accessor :state
STATES.each do |state|
define_method "#{state}?" do
state == self.state
end
end
p garfield.class
# Cat
p garfield.class.ancestors
# [Cat, Animal, Object, Kernel]
p garfield.methods.sort
# ["==", "===", "=~", "__id__", "__send__", "age", "age=", "bigger?", "class", "clone", "display", "dup", "enum_for", "eql?", "equal?", "extend", "freeze", "frozen?", "hash", "id", "inspect", "instance_eval", "instance_exec", "instance_of?", "instance_variable_defined?", "instance_variable_get", "instance_variable_set", "instance_variables", "is_a?", "kind_of?", "method", "methods", "name", "name=", "nil?", "object_id", "older?", "private_methods", "protected_methods", "public_methods", "respond_to?", "send", "singleton_methods", "taint", "tainted?", "tap", "to_a", "to_enum", "to_s", "type", "untaint", "weight="]
p garfield.methods(false)
# []
# Object.const_get("RUBY_VERSION")
class Cat < Animal
def to_s
"I'm #{name}, I have #{age} years and I weigh #{weight} (but I have lost some kg)."
end
end
class Dog < Animal
end
class Animal
attr_accessor :name, :age, :weight
def initialize(name, weight, age)
@name = name
@weight = weight
@age = age
end
def to_s
hulk1.__proto__ = new Monster
hulk1.speak()
=> "grrrr"
// still does'nt work
hulk1.prototype = new Monster();
hulk1.speak()
=> "bonjour"
function Human() { this.speak = function() { return "bonjour" } }
function Monster() { this.speak = function() { return "grrrr" } }
function Hulk() {}
// hulk inherits from Human
Hulk.prototype = new Human()
hulk1 = new Hulk()
hulk1.speak()
=> "bonjour"
var com = {};
com.synbioz = {};
com.synbioz.app = function(name, url) {
this._name = name;
this._url = url;
}
new com.synbioz.app("foo", "foo.com")
function A() {
// private
var _foo = "foo";
function foo() {
return _foo;
};
// public
this._bar = "bar";