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

» JWorld@TW » Languages on JVM » JRuby  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 基本命名規則
cpi





發文: 12
積分: 0
於 2006-03-15 01:02 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
1. Ruby 基本命名規則
a. local 變數和 method, 都是小寫但用 ”_” 連結, 在 rails 中連檔名也是按此規矩
Ex: home, i, note_controller.rb

b. 常數, 全部大寫要用 ’_’ 連結
Ex: PDF_KEY…

c. class 名字也是屬於常數, 但都是開頭大寫其他小寫並且直接連結一起
Ex: AciveRecord, ApplicationController…

d. instance 變數(就是每個 object instance 都有的變數), 要用 ”@” 開頭
Ex: @name, @created_at

e. class 變數(就是每個 object instance 都會共用的變數), 要用 “@@" 開頭
Ex: @@lookup_table, @@count

f. global 變數, 要用 ”$” 開頭
Ex: $global, $:


2. class 寫法請參考下面, initialize 是當你 ClassName.new 時會呼叫到

1
2
3
4
5
6
7
      class NoteRecord
        def initialize(title, description)
          ...
        end
 
        ...
      end

* class method, 則跟 java/c++ static method 一樣

1
2
3
4
5
6
7
            class NoteRecord
              ...
 
              def self.find(t)
                ...
              end
            end

* 完整範例

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
      class Note
        @@db = Array.new
 
        def initialize(title, description)
          @title = title
          @description = description
        end
 
        def self.add(n)
          @@db << n
        end
        def self.find(t)
          @@db.select { |n| 
            n.title == t
          }
        end
      end
 
      n1 = Note.new("Job1", "Do it");
      Note.add(n1);
 
      n2 = Note.new("Job2", "Write it");
      Note.add(n2);
 
      puts "Look for Job2" 
      Note.find("Job2").each { |n|
        puts n.inspect
      }


出自 http://anw.stikipad.com/ocean/show/Basic%2C+Syntax+and+Naming+Rules


cpi edited on 2006-03-15 10:41
reply to postreply to post
作者 Re:基本命名規則 [Re:cpi]
T55555

Java, Ruby, Haskell

版主

發文: 1026
積分: 24
於 2006-03-15 01:57 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
BTW ....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class A
  p self == A
  x = self
  y = A
  
  def self.m1; p "class method #1"; end
  def A.m2;    p "class method #2"; end
  def x.m3;    p "class method #3"; end
  def y.m4;    p "class method #4"; end
end
 
A.m1
A.m2
A.m3
A.m4
 
p A.public_methods.select {|m| m =~ /^m/}.sort


output:
1
2
3
4
5
6
true
"class method #1"
"class method #2"
"class method #3"
"class method #4"
["m1", "m2", "m3", "m4", "method", "method_defined?", "methods", "module_eval"]


reply to postreply to post
» JWorld@TW »  Languages on JVM » JRuby

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

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8