前言

之前用javascript做了可以實現(xiàn) 報名-競價 活動的party bid,如今用ruby語言為party_bid做了個服務(wù)端,可以創(chuàng)建用戶,每個用戶都可以組織自己的party_bid活動,并且設(shè)置的管理員可以看到所有用戶信息,并對這些用戶信息進行管理。
這前后分別學(xué)習(xí)了js與ruby,對它們有了初步的認識,現(xiàn)在對它們之間的共性與不同做個簡單的總結(jié)。
正文
1. 大概簡介
Ruby:Ruby 是一種面向?qū)ο蟆⒚钍健⒑瘮?shù)式、動態(tài)的通用編程語言,其設(shè)計理念是人性化,程序員可以不用寫很多代碼就能實現(xiàn)想要的功能,多用于后臺服務(wù)端。
Javascript:JavaScript 是一種廣泛應(yīng)用于客戶端網(wǎng)頁(瀏覽器)開發(fā)的腳本語言,可以給 HTML 網(wǎng)頁添加動態(tài)功能,響應(yīng)用戶的各種操作,被廣泛用于客戶端。
2. 正式PK
| Array: |
| Ruby: | a = ["1", "2"]
a.push("3")
a.map!(&:to_i) # [1, 2, 3] |
| JS: | var a = ["1", "2"];
a.push("3");
a = a.map(function(n) { return parseInt(n, 10); }); |
遍歷方法的使用:
| Ruby: | a = [1, 2, 3]
a.each {|n| puts n}
#or
a.each do |n|
puts n
end
#or
for i in 0..(a.length - 1) do
puts a[i]
end |
| JS: | var a = [1, 2, 3];
a.forEach(function(n) { console.log(n); })
//or
for (var i = 0; i < a.length; i++) {
console.log(a[i]);
} |
| Strings: |
獲取字符串的索引:
| Ruby: | 'hello'.index('e') # 1
'hello'.rindex('l') # 3 |
| JS: | 'hello'.indexOf('e') // 1
'hello'.lastIndexOf('l') // 3 |
判斷字符串的值:
| Ruby: | if 'hello'.include? 'lo'
puts 'found'
end |
| JS: | if (~'hello'.indexOf('lo')) {
console.log('found');
} |
新建字符串:
| Ruby: | 'hello' * 3 # 'hellohellohello' |
| JS: | (new Array(3 + 1)).join('hello')
// 'hellohellohello' |
| Hash: |
定義(方法一樣):
| Ruby: | h = {}
h['a'] = 1
h['b'] = 2 |
| JS: | var h = {};
h['a'] = 1;
h['b'] = 2; //以下兩個用例中的h即為此處定義的h |
遍歷:
| Ruby: | h.each {|key, value| puts "#{key} #{value}" } |
| JS: | for (key in h) { console.log(key, h[key]); } |
取屬性:
| Ruby: | h.keys # ['a', 'b']
h.has_key?('c') # false |
| JS: | Object.keys(h); // ['a', 'b']
h.hasOwnProperty('c') // false |
取長度:
| Ruby: | h.length # 2 |
| JS: | Object.keys(h).length // 2 |
刪除:
| Ruby: | h.delete("b") |
| JS: | delete h.b |
| Functions: |
定義與調(diào)用:
| Ruby: | #定義
def plus_5(num = 0) num + 5 end
#調(diào)用
plus_5 # 5
plus_5(10) # 15
[5, 10, 15].map { |k| plus_5(k) } # [10, 15, 20] |
| JS: | //定義
function plus_5(num) { return (num || 0) + 5; }
//調(diào)用
plus_5(); // 5
plus_5(10); // 15
[5, 10, 15].map(plus_5); // [10, 15, 20] |
| Math: |
| Ruby: | [-5, -1, -8].max # -1 [-5, 15, 20].reduce(0, &:+) # 30 |
| JS: | Math.max.apply(null, [-5, -1, -8]) // -1
[-5, 15, 20].reduce(function(sum, value) {
return sum + value;
}, 0) // 30 |
| Random: |
| Ruby: | prng = Random.new() prng.rand(5..9) # one of [5, 6, 7, 8, 9] |
| JS: | function rand(a, b) {
return Math.floor(Math.random() * (b-a+1)+a);
}
rand(5, 9); // one of [5, 6, 7, 8, 9] |
總結(jié)
總的簡單來說,與javascript相比,ruby是純面向?qū)ο笳Z言,對局部變量的定義不用加前綴var,ruby對函數(shù)的定義不用function打頭,而是def,在方法中不用括號,返回值直接寫變量名,不用return,并且ruby代碼中沒有標點,看上去更加干凈整潔。
參考http://agentcooper.github.io/js-ruby-comparison/
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
當前名稱:ruby與javascript開發(fā)的簡單比較-創(chuàng)新互聯(lián)
標題URL:http://www.chinadenli.net/article40/iihho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、企業(yè)網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè)、靜態(tài)網(wǎng)站、網(wǎng)站維護、品牌網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容