Skip to content

Instantly share code, notes, and snippets.

def colors(size)
1.upto(size).inject([]) { |colors, index|
r, g, b = yield
colors << "#%02x%02x%02x" % [r, g, b]
}
end
def hsv_to_rgb(h, s, v)
h_i = (h * 6).to_i
f = h * 6 - h_i
[...].forty_two # Retourne le 42éme élément du tableau
var a1 = ["4", "2"]
Array.prototype.size = function() { return this.length; }
var a2 = ["s", "y", "n", "b", "i", "o", "z"]
a1.size()
=> 2
a2.size()
=> 7
var a = true
typeof a
=> "boolean"
a.constructor.name
=> "Boolean"
typeof Boolean
=> "function"
Boolean.constructor.name
=> "Function"
function A() {
// private
var _foo = "foo";
function foo() {
return _foo;
};
// public
this._bar = "bar";
var com = {};
com.synbioz = {};
com.synbioz.app = function(name, url) {
this._name = name;
this._url = url;
}
new com.synbioz.app("foo", "foo.com")
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"
// still does'nt work
hulk1.prototype = new Monster();
hulk1.speak()
=> "bonjour"
hulk1.__proto__ = new Monster
hulk1.speak()
=> "grrrr"
class Animal
attr_accessor :name, :age, :weight
def initialize(name, weight, age)
@name = name
@weight = weight
@age = age
end
def to_s