<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # [X分鐘速成Y](http://learnxinyminutes.com/) ## 其中 Y=ruby 源代碼下載:?[learnruby-zh.rb](http://learnxinyminutes.com/docs/files/learnruby-zh.rb) ~~~ # 這是單行注釋 =begin 這是多行注釋 沒人用這個 你也不該用 =end # 首先,也是最重要的,所有東西都是對象 # 數字是對象 3.class #=> Fixnum 3.to_s #=> "3" # 一些基本的算術符號 1 + 1 #=> 2 8 - 1 #=> 7 10 * 2 #=> 20 35 / 5 #=> 7 # 算術符號只是語法糖而已 # 實際上是調用對象的方法 1.+(3) #=> 4 10.* 5 #=> 50 # 特殊的值也是對象 nil # 空 true # 真 false # 假 nil.class #=> NilClass true.class #=> TrueClass false.class #=> FalseClass # 相等運算符 1 == 1 #=> true 2 == 1 #=> false # 不等運算符 1 != 1 #=> false 2 != 1 #=> true !true #=> false !false #=> true # 除了false自己,nil是唯一的值為false的對象 !nil #=> true !false #=> true !0 #=> false # 更多比較 1 < 10 #=> true 1 > 10 #=> false 2 <= 2 #=> true 2 >= 2 #=> true # 字符串是對象 'I am a string'.class #=> String "I am a string too".class #=> String placeholder = "use string interpolation" "I can #{placeholder} when using double quoted strings" #=> "I can use string interpolation when using double quoted strings" # 輸出值 puts "I'm printing!" # 變量 x = 25 #=> 25 x #=> 25 # 注意賦值語句返回了賦的值 # 這意味著你可以用多重賦值語句 x = y = 10 #=> 10 x #=> 10 y #=> 10 # 按照慣例,用 snake_case 作為變量名 snake_case = true # 使用具有描述性的運算符 path_to_project_root = '/good/name/' path = '/bad/name/' # 符號(Symbols,也是對象) # 符號是不可變的,內部用整數類型表示的可重用的值。 # 通常用它代替字符串來有效地表示有意義的值。 :pending.class #=> Symbol status = :pending status == :pending #=> true status == 'pending' #=> false status == :approved #=> false # 數組 # 這是一個數組 array = [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5] # 數組可以包含不同類型的元素 [1, "hello", false] #=> [1, "hello", false] # 數組可以被索引 # 從前面開始 array[0] #=> 1 array[12] #=> nil # 像運算符一樣,[var]形式的訪問 # 也就是一個語法糖 # 實際上是調用對象的[] 方法 array.[] 0 #=> 1 array.[] 12 #=> nil # 從尾部開始 array[-1] #=> 5 # 同時指定開始的位置和長度 array[2, 3] #=> [3, 4, 5] # 或者指定一個范圍 array[1..3] #=> [2, 3, 4] # 像這樣往數組增加一個元素 array << 6 #=> [1, 2, 3, 4, 5, 6] # 哈希表是Ruby的鍵值對的基本數據結構 # 哈希表由大括號定義 hash = {'color' => 'green', 'number' => 5} hash.keys #=> ['color', 'number'] # 哈希表可以通過鍵快速地查詢 hash['color'] #=> 'green' hash['number'] #=> 5 # 查詢一個不存在地鍵將會返回nil hash['nothing here'] #=> nil # 用 #each 方法來枚舉哈希表: hash.each do |k, v| puts "#{k} is #{v}" end # 從Ruby 1.9開始, 用符號作為鍵的時候有特別的記號表示: new_hash = { defcon: 3, action: true} new_hash.keys #=> [:defcon, :action] # 小貼士:數組和哈希表都是可枚舉的 # 它們可以共享一些有用的方法,比如each, map, count 等等 # 控制流 if true "if statement" elsif false "else if, optional" else "else, also optional" end for counter in 1..5 puts "iteration #{counter}" end #=> iteration 1 #=> iteration 2 #=> iteration 3 #=> iteration 4 #=> iteration 5 # 然而 # 沒人用for循環 # 用`each`來代替,就像這樣 (1..5).each do |counter| puts "iteration #{counter}" end #=> iteration 1 #=> iteration 2 #=> iteration 3 #=> iteration 4 #=> iteration 5 counter = 1 while counter <= 5 do puts "iteration #{counter}" counter += 1 end #=> iteration 1 #=> iteration 2 #=> iteration 3 #=> iteration 4 #=> iteration 5 grade = 'B' case grade when 'A' puts "Way to go kiddo" when 'B' puts "Better luck next time" when 'C' puts "You can do better" when 'D' puts "Scraping through" when 'F' puts "You failed!" else puts "Alternative grading system, eh?" end # 函數 def double(x) x * 2 end # 函數 (以及所有的方法塊) 隱式地返回了最后語句的值 double(2) #=> 4 # 當不存在歧義的時候括號是可有可無的 double 3 #=> 6 double double 3 #=> 12 def sum(x,y) x + y end # 方法的參數通過逗號分隔 sum 3, 4 #=> 7 sum sum(3,4), 5 #=> 12 # yield # 所有的方法都有一個隱式的塊參數 # 可以用yield參數調用 def surround puts "{" yield puts "}" end surround { puts 'hello world' } # { # hello world # } # 用class關鍵字定義一個類 class Human # 一個類變量,它被這個類地所有實例變量共享 @@species = "H. sapiens" # 構造函數 def initialize(name, age=0) # 將參數name的值賦給實例變量@name @name = name # 如果沒有給出age, 那么會采用參數列表中地默認地值 @age = age end # 基本的 setter 方法 def name=(name) @name = name end # 基本地 getter 方法 def name @name end # 一個類方法以self.開頭 # 它可以被類調用,但不能被類的實例調用 def self.say(msg) puts "#{msg}" end def species @@species end end # 類的例子 jim = Human.new("Jim Halpert") dwight = Human.new("Dwight K. Schrute") # 讓我們來調用一些方法 jim.species #=> "H. sapiens" jim.name #=> "Jim Halpert" jim.name = "Jim Halpert II" #=> "Jim Halpert II" jim.name #=> "Jim Halpert II" dwight.species #=> "H. sapiens" dwight.name #=> "Dwight K. Schrute" # 調用對象的方法 Human.say("Hi") #=> "Hi" ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看