ruby學習筆記

RUBY語言學習

幾個難點學習理解

  • ruby語言是比較簡單的一門語言,由於它的一切設計原則是面向對象的,因此,使用起來特別方便。固然,主要是因爲它具備與Python、swift等等類似的語法特色,使得他使用起來十分方便。

下面概括幾個本人平常遇到的問題。

1.關於變量的可讀可寫。

attr_reader : name 表示name只讀
attr_write : name 表示name只寫
attr_accessor: name 表示可讀可寫

2.關於module.

  • module與Class相似,能夠在其中定義方法,而後在其餘類中使用。好比能夠有如下方式的引用。
module M def mm puts "helloworld!" end end class A include M def initalize puts "aa" end end a = A.new a.mm

3.關於關鍵字public以及關鍵字private

  • public表示公共藉口,能夠在其餘類中使用這個類的藉口函數。而private表示只有在自身類中能夠使用,其餘類不能使用。
class Application attr_accessor :status def initialize end public def print_status puts "all system go!" end private def password @password = 123456 end end
相關文章
相關標籤/搜索