Ruby常用方法

2021-07-22 09:54:42 字數 3637 閱讀 7287

count

判斷內容l的個數integer型

full_name.count("l")     //full_name ="full"
include?

判斷內容是否有l,boolean型

full_name.include?("l")   //full_name ="full"
equal?

equal?方法用來判斷2個物件是否是同乙個物件

a.equal? b相當於判斷"a就是b"

2.1.1 :005 > "a" == "a"

=> true

2.1.1 :006 > "a".equal? "a"

=> false

2.1.1 :007 > :a.equal? :a

=> true

size

size內容的個數

full_name.size
upcase、downcase、 swapcase

str="abc"

irb(main):036:0> str.upcase #全部大寫

=> "abc"

irb(main):037:0> str.downcase #全部小寫

=> "abc"

irb(main):038:0> str.swapcase #字母大小寫轉換

=> "abc"

-------

irb(main):067:0> str="abcd"

=> "abcd"

irb(main):068:0> str.swapcase

=> "abcd"

irb(main):069:0> str=str.lstrip

=> "abcd"

irb(main):072:0> puts str.rjust(10,'.')

......abcd

irb(main):073:0> puts str.ljust(10,'.')

abcd......

irb(main):074:0> puts str.center(10,'.')

...abcd...

irb(main):078:0> str.split(//)

=> ["a", "b", "c", "d"]

型別轉換

ruby的整數、浮點數、字串的類均提供了to_i,to_f,to_s三個方法,分別用於轉換成整數、轉換成浮點數、轉換成字串.

浮點數轉換成整數,會強行去掉小數點後面的數字

irb(main):017:0> 123.45.to_i

=> 123

整數轉換成浮點數,會新增小數點和0

irb(main):018:0> 123.to_f

=> 123.0

整數轉換成字串

irb(main):019:0> 123.to_s

=> "123"

浮點數轉換成字串

irb(main):020:0> 123.45.to_s

=> "123.45"

浮點數轉換成字串,會去掉最後多餘的0

irb(main):021:0> 123.1230.to_s

=> "123.123"

字串轉換成整數,以字元開頭的,轉換不了返回0

irb(main):022:0> "sharejs.com-001".to_i

=> 0

以數字開頭的字串轉換成浮點數

irb(main):024:0> "123.45sharejs.com".to_f

=> 123.45

以數字開頭的字串轉換成整數

irb(main):025:0> "123.45sharejs.com".to_i

=> 123

irb(main):094:0> puts :derek.object_id

554568

irb(main):092:0> puts :derek.class

symbol

irb(main):091:0> puts :derek.to_s

derek

puts array_4.join(「,」)

ruby中nil?, empty? , blank?

.nil? 和 .empty? 是 ruby的方法

.blank? 是 rails的方法

.nil? ==> 判斷物件是否存在(nil)。不存在的物件都是nil的

.empty? ==> 物件已經存在,判斷是否為空欄位,比如乙個字串是否為空串,或者乙個陣列中是否有值。

.blank? ===> 相當於同時滿足 .nil? 和 .empty? 。railsapi中的解釋是如果物件是:false, empty, 空白字元. 比如說: "", " ", nil, , 和{}都算是blank。 (object.blank? 相當於 object.nil?||object.empty?)

if !address.nil? && !address.empty? ===> if !address.blank?

.nil

-----------

.nil?

- it is ruby method

- it can be used on any object and

istrue

if the object is nil.

- "only the object nil responds true to nil?" - railsapi

nil.nil? = true

anthing_else.nil? = false

a = nil

a.nil? = true

「」.nil = false

.empty?

- it is ruby method

- can be used on strings, arrays and hashes and returns true

if:string length == 0

array length == 0

hash length == 0

- running .empty? on something that is nil will throw a nomethoderror

"".empty = true

" ".empty? = false

.blank?

- it is rails method

- operate on any object as well as work like .empty? on strings, arrays and hashes.

nil.blank? = true

.blank? = true

{}.blank? = true

"".blank? = true

5.blank? == false

ruby常用記錄

表單 form for article do f f.label title f.text field title f.label text f.text area text f.submit end 呼叫form for方法時,要指定乙個物件。在上面的表單中,指定的是 article。這個物件告訴...

ruby入門 方法

要注意引數,可變長度引數,和引數預設值 還有物件的特殊方法 class person def say1 word1,word2 puts word1 word2 end variable var def say2 word puts word end default var def say3 wor...

ruby 同名方法

第一種情況 兩個版本的方法,其中乙個包含在類中,另一種包含在類的模組中。module a def say puts hello endend class b include a def say puts hello evryone endend b b.new b.say 輸出結果為 hello e...