Created
February 19, 2014 06:09
-
-
Save boddhisattva/9086865 to your computer and use it in GitHub Desktop.
Enumerable Objects in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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