11155
(-- --)
#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

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

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

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?

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

11155
(-- --)
#3
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

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

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