Created
July 31, 2013 06:38
-
-
Save chiragmongia/6119848 to your computer and use it in GitHub Desktop.
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
| #Quetion- Power - Array | |
| #Define a method power() for an array. It takes an argument 'x' and returns the array with elements raised to power 'x'. Try to make use of array functions. | |
| #Eg: [1,2,3,4,5,6].power(3) -> [1, 8, 27, 64, 125, 216] | |
| class Array | |
| def power(power_value) | |
| self.each { |i| p i ** power_value } | |
| end | |
| end | |
| #Main | |
| puts "Enter the value of power" | |
| power_value = gets.chomp | |
| if power_value =~ /[a-z]/i | |
| puts "Value entered is not an integer!" | |
| else | |
| puts "---Array raise to the power #{power_value} is---" | |
| [1,2,3,4,5].power(power_value.to_i) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment