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 Cat | |
| STATES = ["hungry", "sated"].freeze | |
| attr_accessor :state | |
| STATES.each do |state| | |
| define_method "#{state}?" do | |
| state == self.state | |
| end | |
| end |
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
| 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") |
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 Cat < Animal | |
| def to_s | |
| "I'm #{name}, I have #{age} years and I weigh #{weight} (but I have lost some kg)." | |
| end | |
| end |
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 Dog < Animal | |
| end |
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 Animal | |
| attr_accessor :name, :age, :weight | |
| def initialize(name, weight, age) | |
| @name = name | |
| @weight = weight | |
| @age = age | |
| end | |
| def to_s |
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
| hulk1.__proto__ = new Monster | |
| hulk1.speak() | |
| => "grrrr" |
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
| // still does'nt work | |
| hulk1.prototype = new Monster(); | |
| hulk1.speak() | |
| => "bonjour" |
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
| 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" |
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
| var com = {}; | |
| com.synbioz = {}; | |
| com.synbioz.app = function(name, url) { | |
| this._name = name; | |
| this._url = url; | |
| } | |
| new com.synbioz.app("foo", "foo.com") |
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
| function A() { | |
| // private | |
| var _foo = "foo"; | |
| function foo() { | |
| return _foo; | |
| }; | |
| // public | |
| this._bar = "bar"; |