Ruby 與 JavaScript 常用語法對照表

看到 agentcooper 整理的 Ruby 與 JavaScript 的比較表, 裡面把常會用到的 Function 都列出來, 學到很多寫法~ 🙂

Ruby vs JavaScript

下述轉載自此篇: Ruby vs Javascript.

註: 原文有更清楚的連結與顏色標示.

Ruby Javascript

Arrays

a = ["1", "2"]
a.push("3")

a.map!(&:to_i) # [1, 2, 3]

a.delete_at(1)
a # [1, 3]

a.reverse # [3, 1]

a.unshift(777) # [777, 3, 1]
var a = ["1", "2"];
a.push("3");

a = a.map(function(n) { return parseInt(n, 10); });

a.splice(1, 1 /* how much */);
a; // [1, 3]

a.reverse() // [3, 1]

a.unshift(777); // 777
a; // [777, 3, 1]

/* or in place: */ var b = [3, 1];
                   [777].concat(b); // [777, 3, 1]
a = [1, 2, 3]

a.index(2) # 1 

a.all?{|n| n > 4} # false
a.any?{|n| n > 2} # true

a.keep_if{|n| n > 1} # [2, 3]
var a = [1, 2, 3];

a.indexOf(2); // 1

a.every(function(n) { return n > 4; }); // false
a.some(function(n) { return n > 2; });  // true

a.filter(function(n) { return n > 1;}); // [2, 3]
a = [1, 2, 3, 4, 5]

a.slice(1..-2)  # [2, 3, 4]
a[1..-2]        # [2, 3, 4]
var a = [1, 2, 3, 4, 5];

a.slice(1, -1); // [2, 3, 4]
a = [1, 2, 3]

a.each {|n| puts n}

a.each do |n|
    puts n
end

for i in 0..(a.length - 1) do
  puts a[i]
end
var a = [1, 2, 3];

a.forEach(function(n) { console.log(n); })

for (var i = 0; i < a.length; i++) {
  console.log(a[i]);
}

Strings

'hello'.index('e')    # 1
'hello'.rindex('l')   # 3

if 'hello'.include? 'lo' then puts 'found' end

'hello' * 3           # 'hellohellohello'

'a/b/c'.split('/')    # ['a', 'b', 'c']
'hello'.indexOf('e')             // 1
'hello'.lastIndexOf('l')         // 3

if (~'hello'.indexOf('lo')) { console.log('found'); }

(new Array(3 + 1)).join('hello') // 'hellohellohello'

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

Hash

h = {}
h['a'] = 1
h['b'] = 2

h.each {|key, value| puts "#{key} #{value}" }

h.keys # ['a', 'b']
h.has_key?('c') # false

h.length # 2

h.delete("b")
var h = {};
h['a'] = 1;
h['b'] = 2;

for (key in h) { console.log(key, h[key]); }

Object.keys(h); // ['a', 'b']
h.hasOwnProperty('c') // false

Object.keys(h).length // 2

delete h.b

Functions

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]
function plus_5(num) { return (num || 0) + 5; }

plus_5();   // 5
plus_5(10); // 15

[5, 10, 15].map(plus_5); // [10, 15, 20]
def f(*args)
  puts args
end
function f() {
  var args = Array.prototype.slice.call(arguments);
  console.log(args);
}

Classes

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"
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

[-5, -1, -8].max            # -1

[-5, 15, 20].reduce(0, &:+) # 30
Math.max.apply(null, [-5, -1, -8]) // -1

[-5, 15, 20].reduce(function(sum, value) { return sum + value; }, 0) // 30

Other

prng = Random.new()
prng.rand(5..9) # one of [5, 6, 7, 8, 9]

a, b = b, a # switch a and b
function rand(a, b) { return Math.floor(Math.random() * (b - a + 1) + a); }
rand(5, 9); // one of [5, 6, 7, 8, 9]

a = [b, b = a][0] // do not try at home :-)

JavaScript function 參照說明

Ruby function 參照說明

作者: Tsung

對新奇的事物都很有興趣, 喜歡簡單的東西, 過簡單的生活.

在〈Ruby 與 JavaScript 常用語法對照表〉中有 4 則留言

    1. 嗯嗯, 回原網站看吧.
      我主要是作個備份, 原網站比較重要. 🙂
      (WordPress 不會自動縮小, 暫時先不管他了)

發表迴響

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料