Skip to content

Instantly share code, notes, and snippets.

@boddhisattva
Created February 19, 2014 06:09
Show Gist options
  • Save boddhisattva/9086865 to your computer and use it in GitHub Desktop.
Save boddhisattva/9086865 to your computer and use it in GitHub Desktop.
Enumerable Objects in Ruby
#Playing with Enumerable Objects
squares = [1,2,3].collect { |x| x*x }
print("Squares:")
puts(squares)
puts("\n")
#--------------------------------------------------------------------------------------------------------------------------------
print("Odd numbers upto range:\n")
odds = (1..25).select { |x| x%2 != 0 }
puts(odds)
puts("\n")
#--------------------------------------------------------------------------------------------------------------------------------
print("Even numbers upto range:\n")
evens = (1..25).reject { |x| x%2 != 0 }
puts(evens)
puts("\n")
Squares:1
4
9
Odd numbers upto range:
1
3
5
7
9
11
13
15
17
19
21
23
25
Even numbers upto range:
2
4
6
8
10
12
14
16
18
20
22
24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment