An initial impression that I've had of Ruby which has stayed true is that there is often more than one way to do something. Just some random Ruby stuff I'm studying tonight.

Fun with Arrays

Two different ways to create an Array

arr1 = # => []
arr2 = []

Two different ways to push an item into an Array

[].push 'foo' # => ['foo']
[] << 'foo' # => ['foo']

Create an Array and push a String into it

arr1 = 'Foobar' # => ['Foobar']
arr2 = [] << 'Foobar' # => ['Foobar']
arr3 = << 'Foobar' # => ['Foobar']

It's interesting to me that these are the same two statements << 'Foobar'<<('Foobar')

Push all the contents of one array into another

arr1 = [1,2,3]
arr2 = [4,5,6]
arr1.push *arr2 # => [1,2,3,4,5,6]

This is an interesting example from the homepage

cities = %w[ Negril Bogota Caracas Paris Amsterdam Berlin]
visited = %w[ Negril Amsterdam Caracas]
puts "I still need to visit the following cities:", cities - visited

Split a String into an Array

'foobar'.chars.to_a # => ['f','o','o','b','a','r']
'foobar'.split(//) # => ['f','o','o','b','a','r']

String#<< and Array#<<

Both Array and String have a << method. With regards to Array it's basically like push and with String it's a concatenator.

[] << 'one' << 'two' << 'three' # => ['one', 'two', 'three']
'one ' << 'two' # => 'one two'

More info

Thanks for reading! Follow me on Twitter and/or G+ for more.

See something wrong or got something to add/change? Fork this git repo and send me a pull request

If you particularly enjoy my work, I appreciate donations given with


10 February 2013