LoginSignup
1
0

More than 3 years have passed since last update.

ruby のあれは、 node.js でどう書くんだ

Last updated at Posted at 2020-01-29

js むずい

ruby node.js
File.read('file.txt') const fs = require('fs')
fs.readFileSync('file.txt', 'UTF-8')
File.write('file.txt', content) fs.writeFileSync('file.txt', content)
a = [ 0, 1, 2, 3, 4, 5 ]
a.delete_at(3) # => 3
a # =>[ 0, 1, 2, 4, 5 ]
const a = [ 0, 1, 2, 3, 4, 5 ]
a.splice(3, 1) // => [ 3 ]
a // =>[ 0, 1, 2, 4, 5 ]
a = [ 0, 7, 2, 5, 9, 1 ]
a.max # => 9
const a = [ 0, 7, 2, 5, 9, 1 ]
Math.max(a) // => 9
a = [ 1, 2, 3 ]
a.each { |a| puts a }
const a = [ 1, 2, 3 ]
for(let m of a) { console.log(a) }
h = {a: 1, b: 2, c: 3}
h.each { |k, v| puts v }
const o = {a: 1, b: 2, c: 3}
for(let k of Object.keys(o)) {
console.log(o[k])
}
s = '123'
s.to_i # => 123
const s = '123'
parseInt(s, '10') // => 123
o = {a: 1, b: 2, c: 3, d: 4, e: 5}
pluck = [:a, :c, :e].map { |k| [k, o[k]] }.to_h
# => {a: 1, c: 3, e: 5}
const o = {a: 1, b: 2, c: 3, d: 4, e: 5}
const pluck = (({a, c, e}) => ({a, c, e}))(o)
// => {a: 1, c: 3, e: 5}
s = 'asdfasdf'
s.gsub('df', 'DF') # => 'asDFasDF'
const s = 'asdfasdf'
s.replace(/df/g, 'DF') // => 'asDFasDF'
require 'YAML'
y = YAML.load_file('a.yml')
const fs = require('fs')
const YAML = require('yaml')
y = YAML.parse(fs.readFileSync('a.yml', 'utf8')
h = [{d: 3}, {d: 10}, {d: 12}]
h.any? { |r| 9 < r[:d] } # => true
const o = [{d: 3}, {d: 10}, {d: 12}]
o.some((r) => (9 < r.d)) // => true
a = [{d: 30}, {d: 10}, {d: 3}]
index = a.index { |r| r[:d] == 3 } # => 2
const a = [{d: 30}, {d: 10}, {d: 3}]
const index = a.findIndex(r => r.d === 3) // => 2
val = 'AAA'
"embed#{val}value" # => 'embedAAAvalue'
const val = 'AAA'
`embed${val}value` // => 'embedAAAvalue'
a = [1, 2, 3]
b = ['a', 'b', 'c']
zip = a.zip(b)
# => [[1, 'a'], [2, 'b'], [3, 'c']]
const a = [1, 2, 3]
const b = ['a', 'b', 'c']
const zip = a.map((e, i) => [e, b[i]])
// => [[1, 'a'], [2, 'b'], [3, 'c']]
a = ['a', 'b', 'c']
mapped = a.map { |m| m * 2 } # => ['aa', 'bb', 'cc']
const a = ['a', 'b', 'c']
const mapped = a.map(m => (m + m)) // => ['aa', 'bb', 'cc']
a = [1, 2, 3]
sum = a.reduce(0) do |memo, cur|
  memo += cur
  memo
end
# => 6
const a = [1, 2, 3]
const sum = a.reduce((memo, cur) => {
  memo += cur
  return memo
}, 0)
// => 6
a = ['a', 'bb', 'c', 'ddd', 'e']
selected = a.select { |m| m.length == 1 } # => ['a', 'c', 'e']
const a = ['a', 'bb', 'c', 'ddd', 'e']
const selected = a.filter(m => (m.length === 1)) // => ['a', 'c', 'e']
a = ['a', 'b', 'c']
a.map.with_index { |m, idx| "#{m}-#{idx}" } # => ["a-0", "b-1", "c-3"]
const a = ['a', 'b', 'c']
a.map((m, idx) => (m + '-' + idx;)) // => ["a-0", "b-1", "c-3"]
a = [1, 2, 3]
a.clear
a # => []
const a = [1, 2, 3]
a.length = 0
a // => []
a = []
a.empty? # => true
const a = []
a.length === 0 // => true
a = [1, 2, 3]
a.shift # => 1
a.unshift 5 # => [5, 2, 3]
a.pop # => 3
a << 6 # => [5, 2, 6]
const a = [1, 2, 3]
a.shift() // => 1
a.unshift(5)
a.pop() // => 3
a.push(6)
a // => [5, 2, 6]
a = [1, 2, 3]
a.find { |m| 1 < m } # => 2
const a = [1, 2, 3]
a.find(m => (1 < m) } // => 2
a = [1, 2, 3]
a.all? { |m| 0 < m } # => true
const a = [1, 2, 3]
a.every(m => (0 < m)) // => true

つど調べたら追記!!


var arrDeletedItems = array.splice(start[, deleteCount[, item1[, item2[, ...]]]])
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0