I’m new to Ruby so this may be a stupid question. I know that, for example, if I want to convert a String to a Float I can simply call to_f.
However, what if I only know the type I want to convert to at runtime? So for example I would be able to write something like:
klass = Float
converted = klass.from_s '10.25'
Is there such a method that I just haven’t found? Obviously it wouldn’t work for everything but at least for basic number types.
Since you only want to cover some basic number types, this would work:
Another thing that works is using the
Kernel#FloatandKernel#Integermethods like this: