I have code that calculates the nth root of a number. Right now, this method only works with Fixnum, because I defined it inside the Fixnum class. It would be very simple to do
class Float
but it seems unnecessary. I have no idea how to call classes dynamically. I tried:
classes = [Fixnum, Float] classes.each do |x| x.instance_eval do def root(pow) return self ** (1/pow.to_f) end end end
but it didnβt work. How can I do it? Note After the publication, I realized that this may be better for Programmers.SE, since it is theoretical, as well as based on one problem. Feel free to migrate accordingly ...
source share