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


Rubyとの比較とか

-  [JavaScript | MDN](https://developer.mozilla.org/ja/docs/Web/JavaScript)
- [JavaScript初級者から中級者になろう — uhyohyo.net](http://uhyohyo.net/javascript/)
- [文字列操作の比較表: Ruby, Python, JavaScript, Perl, C++](http://0xcc.net/blog/archives/000137.html)
- [配列操作の比較表: Ruby, Python, JavaScript, Perl, C++](http://0xcc.net/blog/archives/000043.html)

---

## 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:rb
'abc'[1] # => 'b'
'abc'.slice(1) # => 'b'
```
```js:js
'abc'.charAt(1) // 'b'
```
[String.prototype.charAt()](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/String/charAt)

### #size #length
```rb:rb
'abc'.size # => 3
'abc'.length # => 3
```
```js:js
'abc'.length // 3
```
[String.length](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/String/length)

### #to_i
```rb:rb
'1'.to_i # => 1
'1.1'.to_i # => 1
```
```js:js
Number('1') // 1
parseInt('1') // 1
parseInt('1.1') // 1
```
[Number](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Number)
[parseInt()](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/parseInt)

### #to_f
```rb:rb
'1'.to_f # => 1.0
'1.1'.to_f # => 1.1
```
```js:js
Number('1') // 1
parseFloat('1') // 1
parseFloat('1.1') // 1.1
```
[Number](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Number)
[parseFloat()](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/parseFloat)

### #split
```rb:rb
'abc'.split('') # => ["a", "b", "c"]
'abc'.chars # => ["a", "b", "c"]
```
```js:js
'abc'.split('') // ["a", "b", "c"]
```
[String.prototype.split()](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/String/split)

### #chomp
```rb:rb
"\na\nb\nc\n".chomp # => "\na\nb\nc"
```
```js:js
"\na\nb\nc\n".trim() // "a\nb\nc"
```
[String.prototype.trim()](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/String/trim)


## Numeric

Ruby | JavaScript
--- | ---
to_s | Number.prototype.toString()
ceil | Math.ceil()
floor | Math.floor()

### #to_s
```rb:rb
1.to_s # => "1"
1.1.to_s # => "1.1"
```
```js:js
Number('1').toString() // "1"
Number('1.1').toString() // "1.1"
```
[Number.prototype.toString()](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Number/toString)

### #ceil
```rb:rb
1.ceil # => 1
1.1.ceil # => 2
```
```js:js
Math.ceil(1) // 1
Math.ceil(1.1) // 2
```
[Math.ceil()](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Math/ceil)

### #floor
```rb:rb
1.floor # => 1
1.9.floor # => 1
```
```js:js
Math.floor(1) // 1
Math.floor(1.9) // 1
```
[Math.floor()](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/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:rb
[1, 2, 3].count # => 3
[1, 2, 3].size # => 3
[1, 2, 3].length # => 3
```
```js:js
[1, 2, 3].length // 3
```
[Array.length](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/length)

### #join
```rb:rb
[1, 2, 3].join('/') # => "1/2/3"
```
```js:js
[1, 2, 3].join('/') // '1/2/3'
```
[Array.prototype.join()](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/join)

### #sort #reverse
```rb:rb
[1, 2, 3].reverse # => [3, 2, 1]
[1, 2, 3].reverse.sort # => [1, 2, 3]
```
```js:js
[1, 2, 3].reverse(); // [3, 2, 1]
[1, 2, 3].reverse().sort(); // [1, 2, 3]
```
[Array.prototype.sort()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)
[Array.prototype.reverse()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse)

### #each
```rb:rb
[1, 2, 3].each { |v| puts v }
```
```js:js
[1,2,3].forEach(function(v) { console.log(v) });
```
[Array.prototype.forEach()](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach)

### #map #collect
```rb:rb
[1, 2, 3].map { |v| v.to_s } # => [ '1', '2', '3' ]
```
```js:js
[1, 2, 3].map(function(v) { return v.toString() }); // [ '1', '2', '3' ]
```
[Array.prototype.map()](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/map)

### #select
```rb:rb
[1, 2, 3].select {|v| v.odd? } # => [1, 3]
```
```js:js
[1, 2, 3].filter(function(v) { return v % 2 != 0 }); // [ 1, 3 ]
```
[Array.prototype.filter()](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)

### #inject #reduce
```rb:rb
[1, 2, 3].inject(&:+) # => 6
```
```js:js
[1, 2, 3].reduce(function(r, v) { return r + v }); // 6
```
[Array.prototype.reduce()](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce)

### #any?
```rb:rb
[1, 2, 3].any? { |v| v.even? } # => true
```
```js:js
[1, 2, 3].some(function(v) { return v % 2 == 0 }); // true
```
[Array.prototype.some()](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/some)

### #all?
```rb:rb
[2, 4, 6].all? { |v| v.even? } # => true
```
```js:js
[2, 4, 6].every(function(v) { return v % 2 == 0 }); // true
```
[Array.prototype.every()](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/every)

### #include?
```rb:rb
[1, 2, 3].include?(2) # => true
```
```js:js
[1, 2, 3].indexOf(2) >= 0 // true
```
[Array.prototype.indexOf()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf)
```js:js
// This is an experimental technology, part of the ECMAScript 2016 (ES7) proposal.
[1, 2, 3].includes(2) // true
```
[Array.prototype.includes()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes)


## Hash

Ruby | JavaScript
--- | ---
keys | Object.keys()
values | ???
each | ???
map | ???
size | ???
empty? | ???
key? #has_key? | ???
value? #has_value? | ???

### #keys
```rb:rb
{a: 1, b: 2}.keys # => [:a, :b]
```
```js:js
Object.keys({a: 1, b: 2}) // [ 'a', 'b' ]
```
[Object.keys()](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/keys)


## Range

### (1..10).to_a

```js
[...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 ガイド](https://developer.mozilla.org/ja/docs/Web/JavaScript/Guide)

## [Statements](https://developer.mozilla.org/ja/docs/Web/JavaScript/Guide/Control_flow_and_error_handling)

### オブジェクト操作文

#### [for ... in](https://developer.mozilla.org/ja/docs/Web/JavaScript/Guide/Control_flow_and_error_handling#for...in_.E6.96.87)

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

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