at(0) # => "a"
??? String#from, String#to, String#first, and String#last work just as their names suggest.
As usual, negative indices count from the end of the string:
"asdf".from(1) # => "sdf"
"asdf".to(1) # => "as"
"asdf".from(1).to(-2) # => "sd"
"asdf".first # => "a"
"asdf".last # => "f"
"asdf".first(2) # => "as"
"asdf".last(2) # => "df"
??? String#to_time (defaults to UTC), String#to_time(:local), and String#to_date are
easy ways to delegate to ParseDate:
"1/4/2007".to_date.to_s # => "2007-01-04"
"1/4/2007 2:56 PM".to_time(:local).to_s # => "Thu Jan 04 14:56:00 CST 2007"
??? String#starts_with?(prefix) and String#ends_with?(suffix) test whether a string
starts with or ends with another string.
"aoeu".starts_with?("ao") # => true
"aoeu".starts_with?("A") # => false
"aoeu".ends_with?("eu") # => true
"aoeu".ends_with?("foo") # => false
Symbol#to_proc symbol.rb
ActiveSupport defines only one extension to Symbol, but it is a powerful one: the ability to
convert a symbol to a Proc using Symbol#to_proc. This idiom is used all over Rails now.
Code such as this:
(1..5).map {|i| i.to_s } # => ["1", "2", "3", "4", "5"]
becomes this, using Symbol#to_proc:
(1..5).map(&:to_s) # => ["1", "2", "3", "4", "5"]
The & symbol tells Ruby to treat the symbol :to_s as a block argument; Ruby knows that
the argument should be a Proc and tries to coerce it by calling its to_proc method.
Pages:
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121