Convert "one" to 1?

Hi

Does anyone know of a quick way to convert human (string) numbers to
integers?

I doesnt need to be too fancy - the simple integers up to ten ( 10 :slight_smile:
will do.

Id prefer not to install a plugin or gem - so the solution is not native
to rails the possibly a ‘.collect’ type algorithm?

Thanks for reading this!

Pieter

On Wed, Apr 13, 2011 at 6:08 PM, Pieter H. [email protected]
wrote:

hmm… i don’t know of any but i’d create a method that goes like

def num_word_to_int(string

[‘zero’,‘one’,‘two’,‘three’,‘four’,‘five’,‘six’,‘seven’,‘eight’,‘nine’,‘ten’].rindex
string
end

To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.

Hi Jim - thanks - I came up with something similar - but using .index.
Cant seem to figure out the different?

Yours is more elegant with the leading ‘zero’ though. Thanks!

Pieter

What’s wrong with Hash ?

{ “one” => 1
“two” => 2

}

You can generate such hash easily and make it a constant.

WordToInteger[“one”] => 1

Robert Pankowecki

On Wed, Apr 13, 2011 at 6:40 PM, Pieter H. [email protected]
wrote:

Hi Jim - thanks - I came up with something similar - but using .index.
Cant seem to figure out the different?

Yours is more elegant with the leading ‘zero’ though. Thanks!

Thanks. glad to be of help :slight_smile:

[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.

Even better!

Thanks guys

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs