本篇內(nèi)容主要講解“Ruby語言實(shí)例代碼分析”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Ruby語言實(shí)例代碼分析”吧!
海港網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),海港網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為海港上1000家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請找那個(gè)售后服務(wù)好的海港做網(wǎng)站的公司定做!
# This is a comment # 這是一行注釋 =begin This is a multiline comment No-one uses them You shouldn't either 多行注釋是這樣寫的, 沒人用它,你也不要用它。 =end # First and foremost: Everything is an object. # 第一條也是最重要的一條:每樣?xùn)|西都是對象。 # Numbers are objects # 數(shù)字是對象 3.class #=> Fixnum # (譯注:`class` 屬性指向?qū)ο笏鶎俚念悺_@里的 Fixnum 即整數(shù)類。) 3.to_s #=> "3" # (譯注:`to_s` 是整數(shù)對象的一個(gè)方法,其作用是轉(zhuǎn)換為字符串。) # Some basic arithmetic # 一些基本運(yùn)算 1 + 1 #=> 2 8 - 1 #=> 7 10 * 2 #=> 20 35 / 5 #=> 7 # Arithmetic is just syntactic sugar # for calling a method on an object # 這些運(yùn)算符實(shí)際上都是語法糖, # 相當(dāng)于在對象上調(diào)用方法 1.+(3) #=> 4 10.* 5 #=> 50 # Special values are objects # 特殊值也是對象 nil # Nothing to see here # 空值 true # truth # 真值 false # falsehood # 假值 nil.class #=> NilClass true.class #=> TrueClass false.class #=> FalseClass # Equality # 等式判斷 1 == 1 #=> true 2 == 1 #=> false # Inequality # 不等式判斷 1 != 1 #=> false 2 != 1 #=> true !true #=> false !false #=> true # apart from false itself, nil is the only other 'falsey' value # 除了 false 本身之外,nil 是剩下的唯一假值 !nil #=> true !false #=> true !0 #=> false # (譯注:這個(gè)毀三觀?。。? # More comparisons # 更多比較操作 1 < 10 #=> true 1 > 10 #=> false 2 <= 2 #=> true 2 >= 2 #=> true # Strings are objects # 字符串當(dāng)然還是對象 'I am a string'.class #=> String "I am a string too".class #=> String # (譯注:用單引號或雙引號來標(biāo)記字符串。) placeholder = "use string interpolation" "I can #{placeholder} when using double quoted strings" #=> "I can use string interpolation when using double quoted strings" # (譯注:這里展現(xiàn)了字符串插入方法。) # print to the output # 打印輸出 puts "I'm printing!" # Variables # 變量 x = 25 #=> 25 x #=> 25 # Note that assignment returns the value assigned # This means you can do multiple assignment: # 請注意,賦值語句會(huì)返回被賦進(jìn)變量的那個(gè)值, # 這意味著你可以進(jìn)行多重賦值: x = y = 10 #=> 10 x #=> 10 y #=> 10 # By convention, use snake_case for variable names # 按照慣例,變量名使用由下劃線串連的小寫字母 # (譯注:因?yàn)榭雌饋硐褚粭l蛇,這種拼寫稱作“snake case”) snake_case = true # Use descriptive variable names # 建議使用描述性的變量名 path_to_project_root = '/good/name/' path = '/bad/name/' # Symbols (are objects) # Symbols are immutable, reusable constants represented internally by an # integer value. They're often used instead of strings to efficiently convey # specific, meaningful values # 符號(也是對象) # 符號是不可修改的、可重用的常量,在內(nèi)部表示為一個(gè)整數(shù)值。 # 它們通常被用來代替字符串,來有效地傳遞一些特定的、有意義的值。 :pending.class #=> Symbol status = :pending status == :pending #=> true status == 'pending' #=> false status == :approved #=> false # Arrays # 數(shù)組 # This is an array # 這是一個(gè)數(shù)組 [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5] # Arrays can contain different types of items # 數(shù)組可以包含不同類型的元素 array = [1, "hello", false] #=> => [1, "hello", false] # Arrays can be indexed # From the front # 數(shù)組可以用索引號來查詢,下面是順序索引查詢 array[0] #=> 1 array[12] #=> nil # Like arithmetic, [var] access # is just syntactic sugar # for calling a method [] on an object # 類似于運(yùn)算符,[var] 這種查詢語法也是語法糖, # 相當(dāng)于在對象上調(diào)用 [] 方法 array.[] 0 #=> 1 array.[] 12 #=> nil # From the end # 下面是逆向索引查詢 array[-1] #=> 5 # With a start and end index # 使用開始和結(jié)束索引來查詢 array[2, 4] #=> [3, 4, 5] # Or with a range # 或者使用范圍來查詢 array[1..3] #=> [2, 3, 4] # Add to an array like this # 用這種方式來向數(shù)組追加元素 array << 6 #=> [1, 2, 3, 4, 5, 6] # Hashes are Ruby's primary dictionary with keys/value pairs. # Hashes are denoted with curly braces: # 哈希表是 Ruby 最主要的字典型名值對數(shù)據(jù)。 # 哈希表用花括號來表示: hash = {'color' => 'green', 'number' => 5} hash.keys #=> ['color', 'number'] # Hashes can be quickly looked up by key: # 哈希表可以通過鍵名來快速查詢: hash['color'] #=> 'green' hash['number'] #=> 5 # Asking a hash for a key that doesn't exist returns nil: # 向哈希表查詢一個(gè)不存在的鍵名會(huì)返回 nil: hash['nothing here'] #=> nil # Iterate over hashes with the #each method: # 使用 #each 方法來迭代哈希表: hash.each do |k, v| puts "#{k} is #{v}" end # Since Ruby 1.9, there's a special syntax when using symbols as keys: # 從 Ruby 1.9 開始,當(dāng)使用符號作為鍵名時(shí),有其特定語法: new_hash = { defcon: 3, action: true} new_hash.keys #=> [:defcon, :action] # Tip: Both Arrays and Hashes are Enumerable # They share a lot of useful methods such as each, map, count, and more # 提示:數(shù)組和哈希表都是可枚舉的。 # 它們擁有很多相似的方法,比如 each、map、count 等等。 # Control structures # 控制結(jié)構(gòu) if true "if statement" # (譯注:條件語句) elsif false "else if, optional" # (譯注:可選的 else if 語句) else "else, also optional" # (譯注:同樣也是可選的 else 語句) end for counter in 1..5 puts "iteration #{counter}" end #=> iteration 1 #=> iteration 2 #=> iteration 3 #=> iteration 4 #=> iteration 5 # HOWEVER # No-one uses for loops # Use `each` instead, like this: # 不過, # 沒人喜歡用 for 循環(huán), # 大家都用 `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 # Functions # 函數(shù) def double(x) x * 2 end # Functions (and all blocks) implcitly return the value of the last statement # 函數(shù)(包括所有的代碼塊)隱式地返回最后一行語句的值 double(2) #=> 4 # Parentheses are optional where the result is unambiguous # 當(dāng)不會(huì)產(chǎn)生歧義時(shí),小括號居然也是可寫可不寫的。 double 3 #=> 6 double double 3 #=> 12 # (譯注:連續(xù)省略小括號居然也可以?。? def sum(x,y) x + y end # Method arguments are separated by a comma # 方法的參數(shù)使用逗號來分隔 sum 3, 4 #=> 7 sum sum(3,4), 5 #=> 12 # yield # All methods have an implicit, optional block parameter # it can be called with the 'yield' keyword # 所有的方法都有一個(gè)隱式的、可選的塊級參數(shù), # 它可以通過 `yield` 關(guān)鍵字來調(diào)用。 def surround puts "{" yield puts "}" end surround { puts 'hello world' } # { # hello world # } # Define a class with the class keyword # 使用 class 關(guān)鍵字來定義類 class Human # A class variable. It is shared by all instances of this class. # 一個(gè)類變量。它將被這個(gè)類的所有實(shí)例所共享。 @@species = "H. sapiens" # Basic initializer # 基本的初始化函數(shù)(構(gòu)造函數(shù)) def initialize(name, age=0) # Assign the argument to the "name" instance variable for the instance # 把參數(shù) `name` 賦值給實(shí)例變量 `@name` @name = name # If no age given, we will fall back to the default in the arguments list. # 如果沒有指定 age,我們會(huì)從參數(shù)列表中獲取后備的默認(rèn)值。 @age = age end # Basic setter method # 基本的 setter 方法 def name=(name) @name = name end # Basic getter method # 基本的 getter 方法 def name @name end # A class method uses self to distinguish from instance methods. # It can only be called on the class, not an instance. # 一個(gè)類方法使用開頭的 `self` 來與實(shí)例方法區(qū)分開來。 # 它只能在類上調(diào)用,而無法在實(shí)例上調(diào)用。 def self.say(msg) puts "#{msg}" end def species @@species end end # Instantiate a class # 實(shí)例化一個(gè)類 jim = Human.new("Jim Halpert") dwight = Human.new("Dwight K. Schrute") # Let's call a couple of methods # 我們試著調(diào)用一些方法 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" # Call the class method # 調(diào)用類方法 Human.say("Hi") #=> "Hi"
到此,相信大家對“Ruby語言實(shí)例代碼分析”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
網(wǎng)站欄目:Ruby語言實(shí)例代碼分析
文章出自:http://chinadenli.net/article32/ppghsc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)公司、外貿(mào)網(wǎng)站建設(shè)、小程序開發(fā)、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)