JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Languages on JVM » JRuby  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 【Ruby】[3] Ruby.exe, irb, ri ( Ruby 三劍客篇 ) [精華]
T55555

Java, Ruby, Haskell

版主

發文: 1026
積分: 24
於 2005-02-23 01:58 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Ruby 的核心是Ruby interpreter 在 Windows 底下是 Ruby.exe program.
在上一篇介紹了 HelloWorld, 不過一離開 irb, 程式也不見了.
您不會想每次重新 coding 吧?

把 HelloWorld 存在一個檔案裡(可用notepad), 例如: HelloWorld.rb
1
2
C:\>type HelloWorld.rb
puts "Hello World"


之後您就可以﹕
(1) Use mouse to double click the HelloWorld.rb to run it.
(2) In Command Prompt, type Ruby.exe HelloWorld.rb
(or just type: HelloWorld
or HelloWorld.rb,
or ruby HelloWorld.rb) to run it.

Scripting language 就是這麼簡單, 無須 compile.
(沒有 class file, object file, exe file, compiler, linker, ... etc, Source code 就是"可執行的程式", 當然您需要 Ruby interpreter)

我稱 Ruby.exe, irb, ri 為 Ruby 三劍客, 老大 Ruby.exe, 也就是核心人物,上面已提到.
左右手... 左青龍 irb 上一篇已提到(它可以讓您很快的試一試 Ruby code, 它也可以讓您知道每一個 expression 的 return value).
現在來介紹右白虎﹕ri

Ruby 雖然有 www.ruby-doc.org, RDoc 等等像 javadoc API 的 Help.
不過如果您用 Ruby 一久, 您會發現開 Browser 去找 API Help 太慢了.
這時 ri 就顯出它的威力.

到 Command prompt 底下打 ri [Enter]
就會自動告訴您如何用.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
C:\>ri
 
ri v1.0.1 - 20041108
 
Usage:
 
  ri [options]  [names...]
 
Display information on Ruby classes, modules, and methods.
Give the names of classes or methods to see their documentation.
Partial names may be given: if the names match more than
one entity, a list will be shown, otherwise details on
that entity will be displayed.
 
Nested classes and modules can be specified using the normal
Name::Name notation, and instance methods can be distinguished
from class methods using "." (or "#") instead of "::".
 
For example:
 
    ri  File
    ri  File.new
    ri  F.n
    ri  zip
 
Note that shell quoting may be required for method names
containing punctuation:
 
    ri 'Array.[]'
    ri compact\!
 
For help on options, type 'ri -h'
For a list of classes I know about, type 'ri -c'


不知道什麼是 puts ?
打 ri puts [Enter]
1
2
3
4
5
C:\>ri puts
More than one method matched your request. You can refine
your search by asking for information on one of:
 
     IO#puts, Kernel#puts, Zlib::GzipWriter#puts

Hello World 的 puts 是 Kernel 的 method (為什麼? 以後有時間再告訴您)
so type "ri Kernel.puts" [Enter] (or Kernel#puts)
1
2
3
4
5
6
7
C:\>ri Kernel.puts
------------------------------------------------------------ Kernel#puts
     puts(obj, ...)    => nil
------------------------------------------------------------------------
     Equivalent to
 
         $stdout.puts(obj, ...)


Mmm... $stdout 是 IO object
so type ri IO.puts [Enter]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
C:\>ri IO.puts
---------------------------------------------------------------- IO#puts
     ios.puts(obj, ...)    => nil
------------------------------------------------------------------------
     Writes the given objects to _ios_ as with +IO#print+. Writes a
     record separator (typically a newline) after any that do not
     already end with a newline sequence. If called with an array
     argument, writes each element on a new line. If called without
     arguments, outputs a single record separator.
 
        $stdout.puts("this", "is", "a", "test")
 
     _produces:_
 
        this
        is
        a
        test


現在您知道 puts 如何用了吧.
看我一步一步的找, 或許您會覺得 ri 太太慢過 開 Browser 找 API help 了...
很難告訴您為什麼, 時間一久您就會體會到為什麼 ri 快過.

自己試一試 ri String, ri Array...
ri String.squeeze
等等.

以下列出一些 Ruby 的 core classes hierarchy. (基本上都可用 ri 找查)
看看就算了, 不必多想.


T55555 edited on 2005-02-23 02:08
reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
6811 [精華] 【Ruby】[3] Ruby.exe, irb, ri ( Ruby 三劍客篇 ) T55555 3324 2005-02-23 01:58
5501 Re:【Ruby】[3] Ruby.exe, irb, ri ( Ruby 三劍客篇 ) T55555 17 2005-02-23 02:06
5307 Re:【Ruby】[3] Ruby.exe, irb, ri ( Ruby 三劍客篇 ) T55555 29 2005-02-23 02:06
5257 Re:【Ruby】[3] Ruby.exe, irb, ri ( Ruby 三劍客篇 ) T55555 22 2005-02-23 02:08
5265 Re:【Ruby】[3] Ruby.exe, irb, ri ( Ruby 三劍客篇 ) T55555 15 2005-02-23 02:09
5937 Re:【Ruby】[3] Ruby.exe, irb, ri ( Ruby 三劍客篇 ) T55555 16 2005-02-23 02:10
» JWorld@TW »  Languages on JVM » JRuby

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8