Lightning talk 2
- 5. Active Support adds
extensions to...
Array, BigDecimal, Class, Date,
DateTime, File, Float, Hash, Integer,
Kernel, Module, Numeric, Object,
Process, Range, String, and Time
Wednesday, June 19, 13
- 6. String
"hello world".truncate(9)
=> "hello ..."
'person'.pluralize
=> "people"
'people'.singularize
=> "person"
"hello world".titleize
=> "Hello World"
"Hello World!!".parameterize
=> "hello-world"
"hello_world".camelize
=> "HelloWorld"
"HelloWorld".underscore
=> "hello_world"
Wednesday, June 19, 13
- 8. [1, 2, 3, 4, 5].second
=> 2
[1, 2, 3, 4, 5].third
=> 3
[1, 2, 3, 4, 5].fourth
=> 4
[1, 2, 3, 4, 5].fifth
=> 5
(1..100).to_a.forty_two
=> 42
["one", "two", "three"].to_sentence
=> "one, two, and three"
[1, 2, 3, 4, 5, 6, 7].in_groups_of(2)
=> [[1, 2], [3, 4], [5, 6], [7, nil]]
Array
Wednesday, June 19, 13
- 9. [1, 2, 3, 4, 5].second
=> 2
[1, 2, 3, 4, 5].third
=> 3
[1, 2, 3, 4, 5].fourth
=> 4
[1, 2, 3, 4, 5].fifth
=> 5
(1..100).to_a.forty_two
=> 42
["one", "two", "three"].to_sentence
=> "one, two, and three"
[1, 2, 3, 4, 5, 6, 7].in_groups_of(2)
=> [[1, 2], [3, 4], [5, 6], [7, nil]]
Array
Wednesday, June 19, 13
- 10. Hash
{ one: "hello", two: "world", three: "yay" }.to_query
=> "one=hello&three=yay&two=world"
{ one: "hello", two: "world", three: "yay" }.slice(:one, :three)
=> {:one=>"hello", :three=>"yay"}
{ one: "hello", two: "world", three: "yay" }.except(:one)
=> {:two=>"world", :three=>"yay"}
Wednesday, June 19, 13
- 11. to_param
["one", "two", "three"].to_param
=> "one/two/three"
class Post < ActiveRecord::Base
def to_param
"#{created_at.strftime("%m-%d-%y")}-
#{title.parameterize}"
end
end
post_path(@post) # => "6-19-13-hello-
world"
Wednesday, June 19, 13