2007年1月10日星期三

Ruby Continue~~


In recipe 1.4, there is a example:
"Three little words".split(/\s+/)
#=>["Three","little","words"]
"Three little words".split(/(\s+)/)
#=>["Three"," ","little"," ","words"]

Here,when I firstly looked at,I confused. But then I think about it carefully,I got way~! In regular expression /(\s+)/, parentheses make spaces stored. Because parentheses in regular expressions, it make the characters of including by parentheses stored in memory,then u can use \1 to reference it after parentheses.



A important tip in recipe 1.5. Special characters are only interpreted in strings delimited by double quotes, or strings created with %{} or %Q{}, but single quotes and %q{} are not.

puts "foo\tbar"
#=>"foo bar"
puts %{foo\tbar}
#=>"foo bar"
puts %Q{foo\tbar}
#=>"foo bar"

puts 'foo\tbar'
#=>"foo\tbar"
puts %q{foo\tbar}
#=>"foo\tbar"




In Recipe 1.6, I got three ways to convert between charactors and values.
1.use ? charactor
?a
#=>97
2.use to access a element of array
'a'.[0]
#=>97
3.from ascii to char,use #chr method
97.chr
#=>"a"

没有评论: