#定义字符串
"abc"
'efg'
%Q{abc} #等价于 ""
%{hahaha} #等价于 ""
%q!efg! #等价于 ''
%!hello! #等价于 ''
Q:"" 和'' 两者之间的区别?
A:"" 中可以嵌入#{}输出表达式的值,或者是character escapes
str = "abc"
puts "this is #{str}" # this is abc
puts 'this is #{str}' # this is #{str}
puts "abc\nedf" # abc
# edf
puts 'abc\nefg' # abc\nefg
Q:ruby中字符编码?
Q:在ruby中puts "中國" 出现"invalid multitype char<us-ASCII>"
A:ruby中的source code 默認的編碼是US-ACSII,不能解析中文。需要把源文件的編碼改為“適合”的方式,繁體是
#encoding: Big5
#new
my_name = "tony"
your_name = String.new(my_name)
puts my_name.object_id #5907780
puts your_name.object_id #5907768
your_name[0] = "m"
puts my_name # tony
puts your_name # mony
#String.try_convert
puts "try_convert"
puts String.try_convert("aBC") #aBC
puts String.try_convert(12345) #nil
puts String.try_convert("12345") #12345
# % format string
puts "%"
puts "str % arg -> string"
puts "%05d" % 123 #00123 formatstring = "%flag with precision name fieldtype"
puts "%0b" % 123 #1111011
puts "%5s: %08x" % ["ID", self.object_id] # ID: 005af846
puts "%-5s: %08X" % ["ID", self.object_id] #ID : 005AF846
puts "%-5<name>s : %08<value>X" % {name: "hash_id", value: self.object_id} #hash_id: 005AF86
# * copies
# + concatenation
# << append if the object is a Fixnum it is considered to be a codepoint in the encoding
# of str and converted to the appropriate character before being appended
# concat
puts "Ha" * 3 #HaHaHa
puts "Ha" + "Ho" #HaHo
puts "Hello" << 119 #Hellow
puts "Hello" << "Tony" #HelloTony
puts "concat"
puts "Hello".concat(" Tony") # Hello Tony
str = "hello"
str << 119
puts str << "Tony" #hellowTony
#[] []=
puts "[],[]="
str = "Hello Ruby"
puts str[-1] # y
puts str[0] # H
puts str[1] #e
puts str[9] #y
puts str[10] #nil
puts str[0..2] #hel
puts str[0...2] #he
puts str[0,2] #he
puts str["e"] #e
str[0..2] = 'haha'
puts str # hahalo Ruby
#ascii_only?
puts "ascii_only?"
puts __ENCODING__ #Big5
puts "ruby".ascii_only? #true
str = "中國"
puts str.ascii_only? #false
#bytes, chars, codepoints
#str.bytes -> enum
#str.bytes{|byte| block} -> str
#str.chars -> enum
#str.chars{|char| block}-> str
#codepoints(integers representation of the characters)
#str.codepoints -> enum
#str.codepoints {|integer| block} -> str
#getbyte
puts "bytes, getbyte"
p "ruby".bytes.to_a #[114,117,98,121]
puts "getbyte"
puts "ruby".getbyte(1) #117
result = []
puts "ruby".bytes {|byte| result << byte} #ruby
p result #[114,117,98,121]
puts "chars"
p "ruby".chars.to_a # ["r", "u", "b", "y"]
result = []
puts "ruby".chars {|char| result << char} #ruby
p result # ["r", "u", "b", "y"]
puts "codepoints"
p "中國".codepoints.to_a # [42148, 45290]
result = []
puts "中國".codepoints {|b| result << b} #中國
p result #[42148, 45290]
#bytesize, length, size
puts "bytesize, length"
puts "ruby".length #4
puts "ruby".bytesize #4
puts "ruby".size #4
puts "中國".length # 2
puts "中國".bytesize # 4
puts "中國".size #2
#capitalize, capitalize!
#capitalize return a copy of str with the first character converted to
# uppcase and the remainder to lowercase
#capitalize! Modified str by converting the first character to uppercase and the
# ramainder to lowercase .Returns nil if no changes are made
puts "capitalize, capitalize! "
str = "hello Ruby"
puts "str = hello Ruby"
puts "str.capitalize = > #{str.capitalize}" #Hello ruby
puts "str = > #{str}" # hello Ruby
puts "str.capitalize! = > #{str.capitalize!}" # Hello Ruby
puts "str = > #{str}" # Hello Ruby
puts "str.capitalize! = > #{str.capitalize!}" # nil
puts "str = > #{str}" # Hello Ruby
#<=>, casecmp
puts "<=> , casecmp"
puts "abc" <=> "Abc" #1
puts "abc".casecmp("Abc") # 0
puts "abc" <=> "ab" #1
puts "abc".casecmp("ab") #1
puts "ab" <=> "abc" #-1
puts "ab".casecmp("abc") #-1
#center
puts "center"
str = "ruby"
puts str.center(4) #ruby
puts str.center(5) #ruby
puts str.center(10, '*') #***ruby***
puts str # ruby
#chr return the first character
puts "chr"
puts "ruby".chr # r
puts "中國".chr # 中
#clear removes the content(but not the associated encoding) of str
puts "clear"
str = "ruby"
puts str.clear #nil
puts str.length #0
#encoding: Big5
puts str.encoding #Big5
#chomp, chomp!
#chomp return new string with given record separator remove from the end of str
#str.chomp(rs = $/) -> string
puts "chomp, chomp!"
str = "ruby"
p str.chomp # ruby
p str.chomp("y") # rub
str = "ruby\n"
p str.chomp # ruby
str = "ruby\n\r"
p str.chomp # ruby\n
str = "ruby\r\n"
p str.chomp # ruby
str = "ru \n by"
p str.chomp # ru \n by
#chop, chop!
#remove last character
puts "chop"
p "ruby".chop #rub
p "ruby\n".chop # ruby
p "ruby\r\n".chop #ruby
p "R".chop.chop # ""
#count, delete, delete!
#str.count(<string>+)->int
#Each string parameter difines a set of characters to count.
#the intersection of these sets defines the characters to count in str
#Any parameter that starts with a caret(^) is negated
#The sequence c1-c2 means all characters between c1 and c2
puts "count"
str = "hello world"
puts str.count "lo" #5 在str 中 有 3 個 l 和 兩個 0 => 3 + 2 = 5
puts str.count "el" #4
puts str.count "lo", "o" #2 [l,o],[o]的交集是 0 在str中出現2個
puts str.count "ej-m" # 4 [e,j,k,l,m] 在str 中有 1個 e 3個l 總共 4
puts "delete"
puts str.delete "lo" # he wrd
puts str # hello world
puts str.delete! "e" # hllo world
puts str # hllo world
#crypt 加密
puts "crypt"
puts "ruby".crypt "ruby"
#downcase, downcase!
puts "downcase downcase!"
puts "ABC".downcase #abc
#dump
#Produces a version of str with all nonprinting characters replaced by \nnn notation
#and all special characters escaped
puts "dump"
p "ABC".dump #"\"ABC"\"
p "ABC" #"ABC"
#empty?
puts "empty?"
puts "hello".empty? # false
puts "".empty? #true
#encode, encode!, encoding, force_encoding
puts "encoding"
str = "ruby"
puts str.encoding #GBK
puts str.encode("utf-8") # ruby
puts str.encoding #GBK
puts str.encode!("utf-8") # ruby
puts str.encoding #utf-8
str.force_encoding("GBK")
puts str.encoding #GBK
#gsub, gsub! 與正則表達式連用搜索字符串
#start_with? end_with?
puts "start_with?, end_with"
puts "Apache".end_with?("ache") # true
puts "ruby code".end_with?("python", "perl", "code") # true
puts "Apache".start_with?("Apa") # true
puts "ruby code".start_with?("python","perl","ruby") #true
#index
puts "index"
puts "hello".index("e") #1
puts "hello".index("a") #nil
puts "hello".index(/[aeiou]/, -3) #4
puts "hello".index("lo",4) #nil 第二參數開始搜索的位置
#insert
puts "insert"
puts "abcd".insert(0,"X") #Xabcd
puts "abcd".insert(-1, "X") #abcdX
puts "abcd".insert(1, "X") #aXbcd
puts "abcd".insert(-2, "X") #abcXd
#intern
#Return the Symbol corresponding to str, creatin the symbol if it did not
#previously exist
puts "intern"
p "ruby".intern #:ruby