Skip to content

Instantly share code, notes, and snippets.

@lucascaton
Last active September 27, 2021 04:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucascaton/21ea5708ccb7ea41ff820b762902627e to your computer and use it in GitHub Desktop.
Save lucascaton/21ea5708ccb7ea41ff820b762902627e to your computer and use it in GitHub Desktop.

String

to_i

# Ruby
'1'.to_1 # 1
// JavaScript
parseInt('1') // 1

Array

<<

# Ruby
list = [1, 2]
list << 3
list # [1, 2, 3]
// JavaScript
list = [1, 2]
list.push(3)
list // [1, 2, 3]
// JavaScript
list = [1, 2]
list.concat(3) // [1, 2, 3]
list // [1, 2]

unshift

# Ruby
list = [2, 3]
list.unshift(1)
list # [1, 2, 3]
// JavaScript
var list = [2, 3];
list.unshift(1);
list // [1, 2, 3]

include

# Ruby
list = [1, 2]
list.include?(1) # true
list.include?(9) # false
// JavaScript
list = [1, 2]
list.includes(1) // true
list.includes(9) // false

last

# Ruby
[1, 2, 3, 4].last # 4
// JavaScript
[1, 2, 3, 4].slice(-1)[0] // 4

to_h

# Ruby
[['cow', '🐮'], ['pig', '🐷']].to_h # { "cow" => "🐮", "pig" => "🐷" }
// JavaScript
Object.fromEntries([['cow', '🐮'], ['pig', '🐷']]) // { cow: '🐮', pig: '🐷' }

all?

# Ruby
[1, 1, 1].all? { |i| i == 1 } # true
[1, 2, 3].all? { |i| i == 1 } # false
// JavaScript
[1, 1, 1].every(i => i === 1) // true
[1, 2, 3].every(i => i === 1) // false

any?

# Ruby
[1, 2, 3].any? { |i| i == 1 } # true
[4, 5, 6].any? { |i| i == 1 } # false
// JavaScript
[1, 2, 3].some(i => i === 1) // true
[4, 5, 6].some(i => i === 1) // false

Hash

#keys

# Ruby
{ a: 1, b: 2 }.keys # => [:a, :b]
// JavaScript
Object.keys({ a: 1, b: 2 }) // => [:a, :b]

#values

# Ruby
{ a: 1, b: 2 }.values # => [1, 2]
// JavaScript
Object.values({ a: 1, b: 2 }) // => [1, 2]

Each pair

# Ruby
{ a: 1, b: 2 }.each { |k, v| puts [k, v].inspect }
// JavaScript
Object.entries({ a: 1, b: 2 }).forEach(([k, v]) => console.log({ k, v }));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment