ARRAYS |
Ruby | Javascript |
1
2
3
| a = ["1", "2"]
a.push("3")
|
1
| a.map!(&:to_i) # [1, 2, 3]
|
1
2
| a.delete_at(1)
a # [1, 3]
|
1
2
3
4
5
6
| a.unshift(777)
# [777, 3, 1]
|
|
1
2
3
| var a = ["1", "2"];
a.push("3");
|
1
| a = a.map(function(n) { return parseInt(n, 10); });
|
1
2
| a.splice(1, 1 /* how much */);
a; // [1, 3]
|
1
2
3
4
5
6
| a.unshift(777); // 777
a; // [777, 3, 1]
/* or in place: */
var b = [3, 1];
[777].concat(b); // [777, 3, 1]
|
|
1
2
3
| a = [1, 2, 3]
a.index(2) # 1
|
1
| a.all?{|n| n > 4} # false
|
1
| a.any?{|n| n > 2} # true
|
1
| a.keep_if{|n| n > 1} # [2, 3]
|
|
1
2
3
| var a = [1, 2, 3];
a.indexOf(2); // 1
|
1
| a.every(function(n) { return n > 4; }); // false
|
1
| a.some(function(n) { return n > 2; }); // true
|
1
| a.filter(function(n) { return n > 1;}); // [2, 3]
|
|
1
2
3
4
| a = ["aaa ", " bbb", " ccc "]
a.map(&:strip)
# ["aaa", "bbb", "ccc"]
|
|
1
2
3
4
| var a = ["aaa ", " bbb", " ccc "]
a.map(function(x) { return x.trim(); }); // ['aaa', 'bbb', 'ccc']
a.map(Function.prototype.call, String.prototype.trim); // ['aaa', 'bbb', 'ccc']
|
|
1
2
3
4
| a = [1, 2, 3, 4, 5]
a.slice(1..-2) # [2, 3, 4]
a[1..-2] # [2, 3, 4]
|
|
1
2
3
4
| var a = [1, 2, 3, 4, 5];
a.slice(1, -1); // [2, 3, 4]
|
|
1
2
3
4
5
6
7
| a = [1, 2, 3]
a.each {|n| puts n}
a.each do |n|
puts n
end
|
1
2
3
| for i in 0..(a.length - 1) do
puts a[i]
end
|
|
1
2
3
4
5
6
7
| var a = [1, 2, 3];
a.forEach(function(n) { console.log(n); })
|
1
2
3
| for (var i = 0; i < a.length; i++) {
console.log(a[i]);
}
|
|
|
STRINGS |
Ruby | Javascript |
1
| 'hello'.rindex('l') # 3
|
1
| if 'hello'.include? 'lo' then puts 'found' end
|
1
| 'hello' * 3 # 'hellohellohello'
|
1
| 'a/b/c'.split('/') # ['a', 'b', 'c']
|
|
1
| 'hello'.indexOf('e') // 1
|
1
| 'hello'.lastIndexOf('l') // 3
|
1
| if (~'hello'.indexOf('lo')) { console.log('found'); }
|
1
| (new Array(3 + 1)).join('hello') // 'hellohellohello'
|
1
| 'a/b/c'.split('/') // ['a', 'b', 'c']
|
|
|
HASH |
Ruby | Javascript |
1
2
3
4
5
| h = {}
h['a'] = 1
h['b'] = 2
h.each {|key, value| puts "#{key} #{value}" }
|
1
| h.has_key?('c') # false
|
|
1
2
3
4
5
| var h = {};
h['a'] = 1;
h['b'] = 2;
for (key in h) { console.log(key, h[key]); }
|
1
| Object.keys(h); // ['a', 'b']
|
1
| h.hasOwnProperty('c') // false
|
1
| Object.keys(h).length // 2
|
|
|
FUNCTIONS |
Ruby | Javascript |
1
2
3
4
5
6
| def plus_5(num = 0) num + 5 end
plus_5 # 5
plus_5(10) # 15
[5, 10, 15].map { |k| plus_5(k) } # [10, 15, 20]
|
1
2
3
4
| def f(*args)
puts args
end
|
|
1
2
3
4
5
6
| function plus_5(num) { return (num || 0) + 5; }
plus_5(); // 5
plus_5(10); // 15
[5, 10, 15].map(plus_5); // [10, 15, 20]
|
1
2
3
4
| function f() {
var args = Array.prototype.slice.call(arguments);
console.log(args);
}
|
|
|
CLASSES |
Ruby | Javascript |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| class Person
attr_accessor :firstName, :lastName
def initialize(firstName, lastName)
@firstName = firstName
@lastName = lastName
end
def fullName
@firstName + " " + @lastName
end
end
john = Person.new("John", "Malkovic")
john.fullName # "John Malkovic"
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| function Person(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
Person.prototype.fullName = function() {
return this.firstName + " " + this.lastName;
}
var john = new Person("John", "Malkovic");
john.fullName(); // "John Malkovic"
|
|
|
MATH |
Ruby | Javascript |
1
| [-5, 15, 20].reduce(0, &:+) # 30
|
|
1
| Math.max.apply(null, [-5, -1, -8]) // -1
|
1
| [-5, 15, 20].reduce(function(sum, value) { return sum + value; }, 0) // 30
|
|
|
MISC.. |
Ruby | Javascript |
1
2
| prng = Random.new()
prng.rand(5..9) # one of [5, 6, 7, 8, 9]
|
1
| a, b = b, a # switch a and b
|
|
1
2
| function rand(a, b) { return Math.floor(Math.random() * (b - a + 1) + a); }
rand(5, 9); // one of [5, 6, 7, 8, 9]
|
|