Skip to content

Instantly share code, notes, and snippets.

@brianewing
Created October 29, 2011 23:38
Show Gist options
  • Save brianewing/1325250 to your computer and use it in GitHub Desktop.
Save brianewing/1325250 to your computer and use it in GitHub Desktop.
Fixnum#power_of?
class Fixnum
def power_of?(i)
self != 0 and Math.log(self.abs, i) % 1 == 0
end
end
puts 8.power_of? 2 # => true
puts 64.power_of? 2 # => true
puts 27.power_of? 3 # => true
puts 27.power_of? 4 # => false
puts 128.power_of? 4 # => false
puts 256.power_of? 4 # => true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment