0

I'm nubbie in rails and ruby In my app i get data from csv and in some fields of quantities value is 100> and etc. But how to check that? and select only integer(float) part? But note! it can be <20, >30, 30< and so over.

2 Answers 2

1

If you have floating point numbers (not just the integer part), you can use a small variant of Justin Ko's solution:

values = ["10.1>", "<20.3", ">30.4", "30.6<"]
values.each do |val|
  puts /(\d+\.\d+)/.match(val)[0].to_f
end

# => Output will be 10.1 20.3 30.4 30.6    
Sign up to request clarification or add additional context in comments.

3 Comments

@pavelby, you can then use puts /(\d+\.?\d*)/.match(val)[0].to_f as the regexp. The numbers will be returned as floats, but this pattern will catch the integers and the floating numbers as well.
but also how to catch such numbers? so if 10 didn't have ><= we go more, but if has go to you solution?
@PavelBY, I am not completely sure I understood your question: however, the regexp in my comment above with catch all the numbers (integers as well as the floating point numbers.)
0

You can use a regex to just get the numeric part of the field.

The regex is simply /(\d+)/.

Here is an example:

values = ['<20', '>30', '30<']
values.each do |val|
    puts /(\d+)/.match(val)[0].to_i
end
# => 20, 30, 30

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.