halogenandtoast / alchemist

A scientific conversion library.
155 stars 28 forks source link

Feature - convert string to number #19

Closed janjiss closed 11 years ago

janjiss commented 11 years ago

In my code I have usecases like this:

walk_data[:distance] = "2"
walk_data[:distance].to_f.meters.to.feet.to_f

This does not look good at all, maybe we can add functionality that provides automatic string conversion to measurment values? If yes, I will look at the code and try to write implementation for that.

Cheers

halogenandtoast commented 11 years ago

I think adding in functionality on String is beyond the scope I had intended, but I have a compromise...

It might make sense to modify the Alchemist::Conversion module to convert self to a numeric value

Alchemist.measurement self.to_f, unit_name, exponent

Then the module could be included into whichever class you wanted as long as it provided to_f. I've never really liked the alchemist automatically pollutes Numeric and I've been thinking of ways to avoid that in the future and letting the user make this choice, the only problem I'd have is the fact that the original type will always be coerced into a float (or whatever type I choose).

janjiss commented 11 years ago

Yeah, I thought it would be an overkill to add Alchemist module to string, but your suggestions seems reasonable. IMHO there is no way around float here.

halogenandtoast commented 11 years ago

It's been a while, but I updated the module so you can mix it into anything that responds to to_f so feel free to mix it into String if you so desire. That being said the Conversion module is not mixed into Numeric by default anymore.