Search code examples
rubynumberswords

English words (ONE, TWO, THREE) to number(1,2,3) in ruby


I am trying to convert words/strings to numbers in Ruby for example:-

ONE => 1 
TWO => 2  
THREE => 3 
FOUR => 4 
etc...

I have seen many examples other way around (numbers to words); however, I am not been able to find an example of how to convert words to numbers. It would be great if I can get any insight or help on this.


Solution

  • I created a hash up-to twenty for all words and only for tens(thirty, forty, fifty etc.). Using Reg-ex took off two words and added them for example twenty two is addition of 20+2=22; right now my script only works till hundred, but it can be extended for numbers over 100 and so on.