Skip to content

Instantly share code, notes, and snippets.

@paulmooring
Created November 25, 2014 21:16
Show Gist options
  • Save paulmooring/1bc774c7867c0430bfce to your computer and use it in GitHub Desktop.
Save paulmooring/1bc774c7867c0430bfce to your computer and use it in GitHub Desktop.
Example of collections in Ruby
#!/usr/bin/env ruby
require 'pry'
# A bit about working with collections in Ruby
txt01_01 = <<EOT
In Ruby every expression, regardless of length or content returns exactly 1
value. In a simple single line case this behavior is unsuprising and intuative
to new Ruby programmers:
1
>> 1
1 + 4
>> 5
"Two" << " strings."
>> "Two strings."
EOT
txt01_02 = <<EOT
For more complex lines of code this behavior can be more suprising:
# The each method will return the entire hash
hsh = {:one => 1, :two => 2, :three => 3, :four => 4, :five => 5}
hsh.each do |k,v|
puts "Key: \#{k}"
puts "Value: \#{v}"
end
=> {:one=>1, :two=>2, :three=>3, :four=>4, :five=>5}
# Defining a method returns a symbol representing the method
def say_hi
"hi"
end
=> :say_hi
EOT
txt01_03 = <<EOT
When using the `pry` repl, you'll always see the return value on the line
following any statement preceeded by `=> `.
[1] pry(main)> :return_value
=> :return_value
Give the repl some code and see what the return values are.
EOT
puts txt01_01
puts "\n\nPush enter to continue."
gets
puts txt01_02
puts "\n\nPush enter to continue."
gets
puts txt01_03
puts "\n\nType `exit` to continue."
pry
txt02_01 = <<EOT
Because everything in Ruby is an object and everything in Ruby returns a value,
you can use any expression as an object (call a method on it or use it as an
argument). It's never required to save the return value to a variable, but it
is sometimes desirable:
# This is running 3.even?
(1 + 2).even?
>> false
# Here we select all the odd numbers from an array, then add 2 to each of
# them, and finally pull out the values divisible by 5. Because only the
# final value is needed, no intermediate steps are saved
num_array = [0, 1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13]
some_numbers = num_array.select do |num|
num.odd?
end.map do |n|
n + 2
end.select do |i|
i.modulo(5) == 0
end
=> [5, 15]
That's a bit of a contrived example and is probably bad style, but it shows the
idea of chaining methods onto the return value. The array from the above has
been loaded, trying chaining some methods together.
`num_array = [0, 1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13]`
EOT
num_array = [0, 1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13]
puts txt02_01
puts "\n\nType `exit` to continue."
pry
txt03_01 = <<EOT
With those basics in mind, let's take a look at one of Ruby's best and most
useful libraries `Enumerable`!
It's best to think of objects such as Arrays or Hashes as collections of data
to be acted on. In many languages it's common to use a `for` or `foreach` loop
to iterate over a collection of data. In Ruby that iteration is replaced by
acting on these collections of data directly.
EOT
txt03_02 = <<EOT
Consider the following code in Perl and then in Ruby:
# Perl code
my @array = (1, 2, 3, 4, 5, 6);
my @return_array = ();
foreach (@array) {
my $val = $_;
# Use modulus to check if even
if (($val % 2) == 0) {
push(@return_array, $val)
}
}
foreach (@return_array) {
print $_ . "\\n"
}
# Ruby code
array = ['one', 'two', 'three']
array.select do |val|
val.even?
end.each do |v|
puts v
end
This Ruby code looks much cleaner than the perl example and that is a direct
result of the object-oriented mindset used with Enumerable. Ruby doesn't
want to iterate over the collection, it wants to ask the collection to do
something. In this case I asked it to select the even numbers.
EOT
txt03_03 = <<EOT
Have a look at what else you can ask Enumerable objects to do here:
http://ruby-doc.org/core-2.1.5/Enumerable.html
`num_array` is Enumerable, so try some of them out.
EOT
puts txt03_01
puts "\n\nPush enter to continue."
gets
puts txt03_02
puts "\n\nPush enter to continue."
gets
num_array = [0, 1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13]
puts txt03_03
puts "\n\nType `exit` to continue."
pry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment