Rails Array Methods

to_sentence (Array)
>> %w[Chris Mark Steven].to_sentence
=> "Chris, Mark, and Steven" 
 
You can pass it two options: :connector and skip_last_comma. They work like a’this:

>> %w[Soap Mocha Chocolate].to_sentence(:connector => '&')
=> "Soap, Mocha, & Chocolate" 
>> %w[Ratatat Interpol Beirut].to_sentence(:skip_last_comma => true)
=> "Ratatat, Interpol and Beirut" 
 
to_param (Array)


>> helper.url_for :controller => 'users', :action => 'show', :id => %w[one two three]
=> "/users/one%2Ftwo%2Fthree" 

in_groups_of (Array)

>> %w[1 2 3 4 5 6 7].in_groups_of(3) { |g| p g }
["1", "2", "3"]
["4", "5", "6"]
["7", nil, nil]

>> %w[1 2 3].in_groups_of(2, ' ') { |g| p g }
["1", "2"]
["3", " "]

>> %w[1 2 3].in_groups_of(2, false) { |g| p g }
["1", "2"]
["3"]

>> %w[1 2 3 4 5 6 7].in_groups_of(3)
=> [["1", "2", "3"], ["4", "5", "6"], ["7", nil, nil]]

Comments

Popular posts from this blog

Interview Question on Ruby on Rails

Inserting and Moving elements inside Ruby Array