• In Ruby’s object-oriented world, we work with objects and methods. Unlike VB .NET, where some subroutines return a value (Functions) and others do not (Subs), all Ruby methods must return a value. When an explicit return statement is not used, the
Let’s get back to our jukebox for a moment (remember the jukebox?). At some pointwe’ll be working on the code that handles the user interface—the buttons that peoplepress to select songs and control the jukebox. We’ll need to associate actions
以前ruby從perl中借鑒了很多東西,其中之一就是全域的魔法變數,在這些變數中,估計沒有沒有哪個有$_更奇特。比如,方法gets有這個一個效果:在返回剛讀到的行資料的同時,也把這行的資料存入$_.如果你調用print方法而沒有帶參數,那麼它將會列印出$_中的內容。如果你在寫if或者while語句時,僅將一個Regex作為條件,那麼這個Regex會自動去匹配$_.這樣的話,我們可以寫出下面這個短小精悍的程式來實現在所有的行中找出那些包含郵箱地址的行。1while gets2 if /[a-zA
• First and foremost, run your scripts with warnings enabled (the -w command-line option).• If you happen to forget a “,” in an argument list—especially to print—you can produce some very odd error messages.• A parse error at the last line of the
irb(main):022:0* 1.next=> 2irb(main):023:0> "a".next=> "b"irb(main):024:0> "1".next=> "2"irb(main):025:0>效能比較:require 'benchmark'n=(1..1000000).to_adef num_next(first,last) while first != last first=first.next end
class Fixnum attr_accessor :valueendx=1p x.object_idx.value="Anleb"p x.value.object_iddef change(x) p x.object_id p x.value.object_id x=2 #指標改變 x.value="Mr.w" p x.object_id p x.value.object_id
上一篇中搭建了Ruby的開發環境,其實那是不完整的,因為僅安裝了Ruby的電腦,並不能真正進行Ruby開發,甚至連最基本的調試功能都不能使用,就像JAVA開發人員只安裝了JRE一樣。為了進行後續開發,我們需要安裝gem,但是在RubyMine中安裝Gem時會報如下錯誤:Please update your PATH to include build tools or download the DevKitfrom 'http://rubyinstaller.org/downloads' and