Skip to content

Instantly share code, notes, and snippets.

@chiragmongia
Created July 31, 2013 06:38
Show Gist options
  • Select an option

  • Save chiragmongia/6119848 to your computer and use it in GitHub Desktop.

Select an option

Save chiragmongia/6119848 to your computer and use it in GitHub Desktop.
#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