Ruby

本页使用了标题或全文手工转换,现处于台湾繁体模式
求聞百科,共筆求聞
Ruby
編程範型物件導向指令式函數式
設計者松本行弘
實作者松本行弘等人
面市時間1995年
穩定版本
3.0.2
(2021年7月7日,​2年前​(2021-07-07
型態系統動態型別
作業系統跨平台
授權條款Ruby授權條款[1]/ BSD授權條款[2]/ GNU通用公眾授權條款第二版[3]
主要實作產品
Ruby MRIYARVRubiniusMagLevJRuby
MacRubyRubyMotionHotRubyIronRuby
mruby
受啟發於
Ada[4]C++[4]CLU[5]Dylan[5]Eiffel[4]
Lisp[5]Perl[5]Python[5]Smalltalk[5]
施影響於
ClojureD[6]ElixirFalconFancy[7]Groovy
Ioke[8]MirahNu[9]ReiaPotion

Ruby 是一種物件導向指令式函數式動態通用程式語言。在20世紀90年代中期由日本電腦科學家松本行弘(Matz)設計並開發,遵守BSD授權條款和Ruby License[10][注 1]。它的靈感與特性來自於PerlSmalltalkEiffelAda以及Lisp語言。由Ruby語言本身還發展出了JRubyJava平台)、IronRuby.NET平台)等基於其他平台的實現。

歷史

Ruby的作者松本行弘

Ruby的作者--松本行弘於1993年2月24日開始編寫Ruby,直至1995年12月才正式公開發佈於fj(新聞群組)。之所以稱為Ruby是取法自Perl,因為Perl的發音與6月的誕生石pearl(珍珠)相同,Ruby選擇以7月的誕生石ruby(紅寶石)命名。

Ruby相較之下比其他類似的程式語言(如PerlPython)年輕,又因為Ruby是日本人發明的,所以早期的非日文資料和程式都比較貧乏,在網上仍然可以找到早期對Ruby的資料太少之類的批評。約於2000年,Ruby開始進入美國,英文的資料開始發展。

2004年,Rails框架誕生,Ruby更加廣為人知,Ruby並於2006年為TIOBE獲選為年度程式語言。

Ruby的理念

減少編程時候的不必要的瑣碎時間,令編寫程式的人高興,是設計Ruby語言的Matz的一個首要的考慮;其次是良好的介面設計。他強調系統設計必須強調人性化,而不是一味從機器的角度設想[11]

遵循著最小驚訝原則,Ruby語言通常非常直觀,按照編程人認為它應該的方式執行。

Ruby的作者認為Ruby > (Smalltalk + Perl) / 2[來源請求],表示Ruby是一個語法像Smalltalk一樣完全物件導向、指令碼執行、又有Perl強大的文字處理功能的程式語言。

Ruby的版本體系

Ruby版本號的構成形式是(MAJOR).(MINOR).(TEENY),均為只有1位的整數;如「1.8.6」、「1.9.3」。

1.9版系統的TEENY不小於1時為穩定版,TEENY為0的版本是開發版。在1.9之前的版本中偶數MINOR代表穩定版,奇數MINOR代表開發版。[12]

Ruby的Hello World程式

下面是一個在標準輸出裝置上輸出Hello World的簡單程式:

#!/usr/bin/env ruby
puts "Hello, world!"

或者是在irb互動式命令列的模式下:

>>puts "Hello, world!"
Hello, world!
=> nil

Ruby的特點

變數與函式的命名規則

乍看之下與Perl的命名規則有些類似,不過Perl的命名用來區分純量、陣列與對映;而Ruby的命名規則用來表示變數與作用域的關係。Ruby的變數有以下幾種:

  • 一般小寫字母、底線開頭:變數(Variable)。
  • $開頭:全域變數(Global variable)。
  • @開頭:實例變數(Instance variable)。
  • @@開頭:類別變數(Class variable)類別變數被共享在整個繼承鏈中
  • 大寫字母開頭:常數(Constant)。

有些函式則會加一個後綴,用來表示函式的用法,跟變數命名規則不同,函式的命名規則只是習慣,不具強制性,即使你不照規則命名也不影響程式運作

  • =結尾:賦值方法,相當於其他程式語言的set開頭的方法,算是一種語法糖
  • !結尾:破壞性方法,呼叫這個方法會修改本來的物件,這種方法通常有個非破壞性的版本,呼叫非破壞性的版本會回傳一個物件的副本。
  • ?結尾:表示這個函式的回傳值是個布林值。

多種字串表示法

Ruby提供了多種字串的表示方法,方便撰寫有大量文字資料的程式。除了來自C語言的引號表示法之外,還有來自於Perl的百分號字面量記法,以及方便書寫大量內容的Heredoc記法。Ruby可以方便地以#{variable_name}的方式向字串中插入變數。

a = '\n这是一个单引号的字符串,反斜线和变量插值不会被转义'

b = %q{这是一个不可转义的字符串}

c = "\n这是一个双引号的字符串,反斜线和变量插值会被转义\n#{a}"

d = %Q{\n這是一個常量字串,特殊内容同样会被转义\n}

e = <<BLOCK
这是一个以Heredoc方式书写的常量字符串,可转义,结尾标志不可缩进
BLOCK

f = <<-BLOCK
      这是一个可以缩进的Heredoc字符串
    BLOCK

g = <<~BLOCK
      这是一个可以缩进的Heredoc字符串
      缩进会被自动去掉,在2.3版本中引入
    BLOCK

h = %/\t这是一个可转义的的字符串\n/

動態修改物件、類別

Ruby是動態語言,你可以在程式中修改先前定義過的類別。 也可以在某個類別的實例中定義該實例特有的方法,這叫做原型方法(prototype)。

class MyClass
  def the_method
    "general method"
  end
end

mc = MyClass.new
def mc.the_method
  "special for this instance."
end

mc.the_method

強大的反射機制與元程式設計

Ruby的反射功能相當驚人,甚至可以自行追蹤程式運作,或是取出private變數、攔截方法的呼叫。 常常與『可以動態的修改物件』這項特色結合,作為『元程式設計』的功能:程式在運行時, 可以由程式設計師提供的資訊,自行生成、修改類別或物件,這項功能大大的提高了撰寫程式碼的效率。 在Rails之中,就大量使用了這種特性。

以下為用Rails使用元程式設計的範例:

class Project < ActiveRecord::Base
  belongs_to :portfolio
  has_one    :project_manager
  has_many   :milestones
end

在這個例子中,Project類別繼承Base類別,Base類別內建的belongs_tohas_onehas_many方法,便會根據參數來修改Project類別的內容,並自行建立其他相關的方法。程式設計師可以更專心處理程式的運作,而不必為每個類別重複得撰寫程式碼。

豐富靈活的迴圈表示

# 使用 for,在 1 到 3 取出值 1、2、3 到 outer_i 裡操作。
for outer_i in 1..3 do
  puts "for: #{outer_i * 100}"
end

# 在 1 到 3 的集合裡針對每個值,放到 i 裡操作。
(1..3).each do |i|
  puts "each: #{i * 100}"
end

# 只要符合 outer_i 小於等於 300,則進入迴圈。
outer_i = 100
while outer_i <= 300
  puts "while: #{outer_i}"
  outer_i += 100
end

# 直到 outer_i 大於 300 前,都可以進入迴圈。
outer_i = 100
until outer_i > 300
  puts "until: #{outer_i}"
  outer_i += 100
end

# 無限迴圈,用 break 來打斷迴圈。
outer_i = 100
loop do
  break if outer_i > 300

  puts "loop: #{outer_i}"
  outer_i += 100
end

# 作 3 次迴圈,i 從 0 開始遞增 1。
3.times do |i|
  puts "times: #{(i + 1) * 100}"
end

# 從 1 遞增 1 到 3,值傳入 i 來操作。
1.upto(3) do |i|
  puts "upto: #{i * 100}"
end

# 從 3 遞減 1 到 1,值傳入 i 來操作。
3.downto(1) do |i|
  puts "downto: #{400 - i * 100}"
end

# 從 100 開始以每步 +100 邁向 300。
100.step(300, 100) do |i|
  puts "step: #{i}"
end

其他特色

  • 完全物件導向:任何東西都是物件,沒有基礎型別
  • 變數是動態類型
  • 任何東西都有值:不管是四則運算、邏輯表達式還是一個語句,都有回傳值。
  • 運算子多載
  • 垃圾回收
  • 強型別[13]
  • 不必事先宣告變數
  • Windows上,載入DLL

比較

  • CPAN上排名第一名,同時也是Perl 6的開發者的唐鳳(Autrijus / Audrey)說:「Ruby就是『沒有到處打廣告的Perl 6』」。[14][15]
  • 松本行弘在接受歐萊禮(O'Reilly)訪問時,提到「Ruby借用了很多Perl的東西……,Python遠比Perl要少……」、「我認為Ruby這個名字作為Perl之後的一門語言的名字真是再恰當不過了。」[16]
  • Perl之父拉里·沃爾(Larry Wall)說:「很多方面上我還是很喜歡Ruby的,這是因為那些部分是從Perl借過去的。:-)」、「我還喜歡Ruby的C<*>一元星號運算子,所以我把它加到Perl 6裡面。」[17]

程式範例

下面的代碼可以在Ruby shell中執行,比如irb互動式命令列,或者儲存為檔案並執行命令ruby <filename>

  • 一些基本的Ruby代碼:
# Everything, including a literal, is an object, so this works:
-199.abs                                                # 199
"ruby is cool".length                                   # 12
"Rick Astley".index("c")                                # 2
"Nice Day Isn't It?".downcase.split(//).sort.uniq.join  # " '?acdeinsty"
  • 一些轉換:
puts "What's your favorite number?"
number = gets.chomp
outputnumber = number.to_i + 1
puts outputnumber.to_s + ' is a bigger and better favorite number.'

集合

a = [1,'hi', 3.14, 1, 2, [4, 5]]

p a[2]           # 3.14
p a.[](2)# 3.14
p a.reverse      # [[4, 5], 2, 1, 3.14, 'hi', 1]
p a.flatten.uniq # [1, 'hi', 3.14, 2, 4, 5]
  • 構造和使用關聯陣列:
hash = { :water => 'wet', :fire => 'hot' }
puts hash[:fire] # Prints:  hot

hash.each_pair do |key, value| # Or:  hash.each do |key, value|
puts "#{key} is #{value}"
end

# Prints:  water is wet
#          fire is hot

hash.delete :water # Deletes :water => 'wet'
hash.delete_if {|k,value| value=='hot'} # Deletes :fire => 'hot'

區塊和迭代器

  • 有兩個語法用於建立區塊:
{ puts "Hello, World!" } # Note the { braces }
#or
do puts "Hello, World!" end
  • 傳參數的區塊使用閉包Closure
# In an object instance variable (denoted with '@'), remember a block.
def remember(&a_block)
  @block = a_block
end

# Invoke the above method, giving it a block which takes a name.
remember {|name| puts "Hello, #{name}!"}

# When the time is right (for the object) -- call the closure!
@block.call("Jon")
# => "Hello, Jon!"
  • 從方法中返回閉包:
def create_set_and_get(initial_value=0) # Note the default value of 0
  closure_value = initial_value
  return Proc.new {|x| closure_value = x}, Proc.new { puts closure_value }
end

setter, getter = create_set_and_get  # ie. returns two values
setter.call(21)
getter.call # => 21
  • 迭代呼叫呼叫時提供的區塊:
def use_hello
  yield "hello"
end

# Invoke the above method, passing it a block.
use_hello {|string| puts string} # => 'hello'
  • 使用區塊迭代陣列:
array = [1, 'hi', 3.14]
array.each { |item| puts item }
# => 1
# => 'hi'
# => 3.14

array.each_index { |index| puts "#{index}: #{array[index]}" }
# => 0: 1
# => 1: 'hi'
# => 2: 3.14

(3..6).each { |num| puts num }
# => 3
# => 4
# => 5
# => 6

像inject()方法可以接收一個參數和一個區塊。迭代的注入列表的每一個成員,執行函式時儲存總和。這同函式程式語言中的foldl函式相類似,比如:

[1,3,5].inject(10) {|sum, element| sum + element} # => 19

首先區塊接收到了10(inject的參數)當作變數sum,並且1(陣列的第一個元素)當作變數element;這會返回11。11又被當作下一步的sum變數,它加上3得到了14。14又被加上了5,最終返回結果19。

  • 區塊執行在內建的方法中:
File.open('file.txt', 'w') do |file| # 'w' denotes "write mode".
  file.puts 'Wrote some text.'
end                                  # File is automatically closed here

File.readlines('file.txt').each do |line|
puts line
end
# => Wrote some text.
  • 使用列舉元和塊求1到10的平方:
(1..10).collect {|x| x*x} # => [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

類別

下面的代碼定義一個命名為Person的類別。含有一個「initialize」方法,用於構選建立一個新物件,它還有兩個方法,一個多載了<=>比較運算子(這樣Array#sort可以使用age排序)另一個多載了to_s方法(這樣Kernel#puts可以格式化輸出),attr_reader是Ruby中元資料編程的例子:attr_accessor為實例變數定義了getter和setter方法,attr_reader只是一個getter方法。另外,方法中最後的聲明是它的返回值,也允許顯式的使用「return」語句。

class Person
  attr_reader :name, :age
  def initialize(name, age)
    @name, @age = name, age
  end
  def <=>(person) # Comparison operator for sorting
    @age <=> person.age
  end
  def to_s
    "#@name (#@age)"
  end
end

group = [
  Person.new("Bob", 33),
  Person.new("Chris", 16),
  Person.new("Ash", 23)
]

puts group.sort.reverse
  • 下面按age倒序輸出了三個名字:
Bob(33)
Ash(23)
Chris(16)

各種版本

Matz's Ruby interpreter, 最初也是最常見的Ruby版本, 簡稱MRI, 用C語言撰寫.

JRuby,類似PythonJython,一個可於Java上執行Ruby的語言,支援Java的介面和類別。最新發布版爲9.1.6.0(2016-11-09),與Ruby 2.3相容。它的官方網站為jruby.org

mruby是一個輕量級的Ruby直譯器,可以嵌入到其它應用程式中,或者作為庫連結到應用中。

參見

注釋

  1. Ruby早期遵守GPL協定。

參考文獻

  1. COPYING in Ruby official source repository. [2013-10-30]. 
  2. BSDL in Ruby official source repository
  3. Contents of /trunk/GPL. [2015-05-02]. 
  4. 4.0 4.1 4.2 Cooper, Peter. Beginning Ruby: From Novice to Professional. Beginning from Novice to Professional 2nd. Berkeley: APress. 2009: 101. ISBN 1-4302-2363-4. To a lesser extent, Python, LISP, Eiffel, Ada, and C++ have also influenced Ruby. 
  5. 5.0 5.1 5.2 5.3 5.4 5.5 Bini, Ola. Practical JRuby on Rails Web 2.0 Projects: Bringing Ruby on Rails to Java. Berkeley: APress. 2007: 3. ISBN 1-59059-881-4. It draws primarily on features from Perl, Smalltalk, Python, Lisp, Dylan, and CLU. 
  6. Intro – D Programming Language 1.0 – Digital Mars. [2013-10-30]. 
  7. Bertels, Christopher. Introduction to Fancy. Rubinius blog. Engine Yard. 2011-02-23 [2011-07-21]. 
  8. Bini, Ola. Ioke. Ioke.org. [2011-07-21]. inspired by Io, Smalltalk, Lisp and Ruby 
  9. Burks, Tim. About Nu™. Programming Nu™. Neon Design Technology, Inc. [2011-07-21]. 
  10. Ruby License. [2004-09-25]. 
  11. The Philosophy of Ruby, A Conversation with Yukihiro Matsumoto, Part I by Bill Venners on 2003-09-29 (Artima Developer,英文)
  12. [Ruby語言入門東南大學出版社2010年4月第一版P19]
  13. To Ruby From Python
  14. Perl 6於2019年10月更名為Raku
  15. 存档副本. [2013-10-22]. 
  16. O'Reilly訪問Matz。. [2007-07-20]. 
  17. Larry Wall On Perl, Religion, and……. [2007-07-20]. 
  18. Guido談Python 3000及與Ruby的競爭。

外部連結