Edited at

RubyのメソッドとJavaScriptで似たようなことをする対応【WIP】

Rubyとの比較とか



String

Ruby
JavaScript

[] slice
String.prototype.charAt()

size length
String.length

to_i
Number() parseInt()

to_f
Number() parseFloat()

split
String.prototype.split()

chomp
String.prototype.trim()

chop
???


[] #slice


rb

'abc'[1] # => 'b'

'abc'.slice(1) # => 'b'


js

'abc'.charAt(1) // 'b'


String.prototype.charAt()


#size #length


rb

'abc'.size # => 3

'abc'.length # => 3


js

'abc'.length // 3


String.length


#to_i


rb

'1'.to_i # => 1

'1.1'.to_i # => 1


js

Number('1') // 1

parseInt('1') // 1
parseInt('1.1') // 1

Number

parseInt()


#to_f


rb

'1'.to_f # => 1.0

'1.1'.to_f # => 1.1


js

Number('1') // 1

parseFloat('1') // 1
parseFloat('1.1') // 1.1

Number

parseFloat()


#split


rb

'abc'.split('') # => ["a", "b", "c"]

'abc'.chars # => ["a", "b", "c"]


js

'abc'.split('') // ["a", "b", "c"]


String.prototype.split()


#chomp


rb

"\na\nb\nc\n".chomp # => "\na\nb\nc"



js

"\na\nb\nc\n".trim() // "a\nb\nc"


String.prototype.trim()


Numeric

Ruby
JavaScript

to_s
Number.prototype.toString()

ceil
Math.ceil()

floor
Math.floor()


#to_s


rb

1.to_s # => "1"

1.1.to_s # => "1.1"


js

Number('1').toString() // "1"

Number('1.1').toString() // "1.1"

Number.prototype.toString()


#ceil


rb

1.ceil # => 1

1.1.ceil # => 2


js

Math.ceil(1) // 1

Math.ceil(1.1) // 2

Math.ceil()


#floor


rb

1.floor # => 1

1.9.floor # => 1


js

Math.floor(1) // 1

Math.floor(1.9) // 1

Math.floor()


Array

Ruby
JavaScript

count size length
Array.length

join
Array.prototype.join()

sort
Array.prototype.sort()

reverse
Array.prototype.reverse()

each
Array.prototype.forEach()

map collect
Array.prototype.map()

select
Array.prototype.filter()

inject #reduce
Array.prototype.reduce()

any?
Array.prototype.some()

all?
Array.prototype.every()

include?
Array.prototype.indexOf()

include?
Array.prototype.includes()

delete
???

empty?
???

first
???

last
???

push
???

pop
???

unshift
???

shift
???

compact
???

uniq
???


#count #size #length


rb

[1, 2, 3].count # => 3

[1, 2, 3].size # => 3
[1, 2, 3].length # => 3


js

[1, 2, 3].length // 3


Array.length


#join


rb

[1, 2, 3].join('/') # => "1/2/3"



js

[1, 2, 3].join('/') // '1/2/3'


Array.prototype.join()


#sort #reverse


rb

[1, 2, 3].reverse # => [3, 2, 1]

[1, 2, 3].reverse.sort # => [1, 2, 3]


js

[1, 2, 3].reverse(); // [3, 2, 1]

[1, 2, 3].reverse().sort(); // [1, 2, 3]

Array.prototype.sort()

Array.prototype.reverse()


#each


rb

[1, 2, 3].each { |v| puts v }



js

[1,2,3].forEach(function(v) { console.log(v) });


Array.prototype.forEach()


#map #collect


rb

[1, 2, 3].map { |v| v.to_s } # => [ '1', '2', '3' ]



js

[1, 2, 3].map(function(v) { return v.toString() }); // [ '1', '2', '3' ]


Array.prototype.map()


#select


rb

[1, 2, 3].select {|v| v.odd? } # => [1, 3]



js

[1, 2, 3].filter(function(v) { return v % 2 != 0 }); // [ 1, 3 ]


Array.prototype.filter()


#inject #reduce


rb

[1, 2, 3].inject(&:+) # => 6



js

[1, 2, 3].reduce(function(r, v) { return r + v }); // 6


Array.prototype.reduce()


#any?


rb

[1, 2, 3].any? { |v| v.even? } # => true



js

[1, 2, 3].some(function(v) { return v % 2 == 0 }); // true


Array.prototype.some()


#all?


rb

[2, 4, 6].all? { |v| v.even? } # => true



js

[2, 4, 6].every(function(v) { return v % 2 == 0 }); // true


Array.prototype.every()


#include?


rb

[1, 2, 3].include?(2) # => true



js

[1, 2, 3].indexOf(2) >= 0 // true


Array.prototype.indexOf()

js:js

// This is an experimental technology, part of the ECMAScript 2016 (ES7) proposal.

[1, 2, 3].includes(2) // true



Array.prototype.includes()


Hash

Ruby
JavaScript

keys
Object.keys()

values
???

each
???

map
???

size
???

empty?
???

key? #has_key?
???

value? #has_value?
???


#keys


rb

{a: 1, b: 2}.keys # => [:a, :b]



js

Object.keys({a: 1, b: 2}) // [ 'a', 'b' ]


Object.keys()


Range


(1..10).to_a

[...Array(10).keys()]

// => [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

Array.from([...Array(10).keys()], (v, i) => i + 1)
// => [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]



JavaScript ガイド


Statements


オブジェクト操作文


for ... in

var obj = {a: 1, b: 2};

for (var key in obj) {
console.log(key + '=[' + obj[key] + ']');
};
// a=[1]
// b=[2]

var obj = [1, 2];

for (var idx in obj) {
console.log(idx + '=[' + obj[idx] + ']');
};
// 0=[1]
// 1=[2]