Created
April 21, 2010 09:10
-
-
Save alexrothenberg/373603 to your computer and use it in GitHub Desktop.
This file contains 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
# In DOS type | |
### cd \workshop | |
### mkdir ruby_exercises | |
### cd ruby_exercises | |
# Open the e editor | |
### e . | |
# Create a new file called array_exercise.rb | |
################################################### | |
### EXERCISE 1 | |
#Take a string an make it lower case | |
def exercise1 my_array | |
my_array.uniq.sort | |
end | |
puts '### EXERCISE 1' | |
puts exercise1 ['Matt', 'Satty', 'Matt', 'Alex', 'Matt'] | |
puts exercise1(['Rwanda', 'Uganda', 'Burundi']) | |
puts exercise1 [10, 55, 3, 1589, 1, 55] | |
################################################### | |
### EXERCISE 2 | |
#Iterate through an array and print each element | |
def exercise2 my_array | |
my_array.each do |element| | |
puts element | |
end | |
end | |
puts '### EXERCISE 2' | |
exercise2 ['Matt', 'Satty', 'Matt', 'Alex', 'Matt'] | |
exercise2(['Rwanda', 'Uganda', 'Burundi']) | |
exercise2 [10, 55, 3, 1589, 1, 55] | |
################################################### | |
################################################### | |
### EXERCISE 3 | |
#Iterate through an array and return an array with each element uppercased | |
def exercise3 my_array | |
my_array.map do |element| | |
element.upcase if element.respond_to? :upcase | |
end | |
end | |
puts '### EXERCISE 3' | |
puts exercise3 ['Matt', 'Satty', 'Matt', 'Alex', 'Matt'] | |
puts exercise3(['Rwanda', 'Uganda', 'Burundi']) | |
puts exercise3 [10, 55, 3, 1589, 1, 55] | |
################################################### | |
### EXERCISE 4 | |
# Convert an array to a comma separated string | |
def exercise4 my_array | |
my_array.uniq.sort.join(', ') | |
end | |
puts '### EXERCISE 4' | |
exercise4 ['Matt', 'Satty', 'Matt', 'Alex', 'Matt'] | |
exercise4(['Rwanda', 'Uganda', 'Burundi']) | |
exercise4 [10, 55, 3, 1589, 1, 55] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment