看板 Ruby 關於我們 聯絡資訊
※ 引述《AIGecko (壁虎貓耳控)》之銘言: [...] : 其實我也有個解決方法 : 只是我不確定會不會很奇怪 : (以自己角度?以大家角度?) : 就是使用類別變數當實體變數用 : 不需要產生實體 : class C : @@value : def self.func : return @@value : end : end : 這種寫法我也不知道該怎麼解釋 : 能解決問題倒是真的 : 或許只要能解決問題+能讓人看懂就好? 其實真的就是這樣就好了呀 :D 除了 @@value 可以換成 @value 就好了。 singleton 在某方面而言會存在,是因為在那些語言中,class 不是 first order value, 所以你一定要先有一個 class, 才能產生一個 value. 而在 ruby 中, class/module 本來就是 first order value 了,也就是說,他們本來就是 singleton 了,所以自然是沒必要在上面再掛一層了... 很多 design pattern 在 ruby 裡都是沒有意義的 -- #!/usr/bin/env ruby [露比] /Programming (Kn|N)ight/ 看板《Ruby》 # if a dog nailed extra legs that http://www.ptt.cc/bbs/Ruby/index.html # walks like an octopus, and Welcome ~Ruby@ptt~ # talks like an octopus, then ◢█◣ http://www.ruby-lang.org/ # we are happy to treat it as http://www.ruby-doc.org/ # if it were an octopus. http://www.rubyforge.org/ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.135.38.235
AIGecko:Ruby果然是讓人寫起來很快樂的語言 09/26 16:42