0

How to get a value from a string e.g

search_params[:price] = "1460,4500"

How can I get the first number into one variable and second into a different variable?

1
  • 1
    v1, v2 = search_params[:price].scan /\d+/ #=> ["1460", "4500"] is one way. Commented Sep 5, 2014 at 7:01

2 Answers 2

2

Did you mean this??:

first_price, second_price = search_params[:price].split(',')
Sign up to request clarification or add additional context in comments.

2 Comments

I am glad it helped you. If an answer helps you on stackoverflow then kindly accept it by clicking on the check icon on the left side of the answer.
will do, it says I have to wait for 10 mins
0

You can use split method

irb(main):002:0> price = "1460,4500"
=> "1460,4500"
irb(main):003:0> price.split(',')
=> ["1460", "4500"]
irb(main):004:0> a, b = price.split(',')
=> ["1460", "4500"]
irb(main):005:0> a
=> "1460"
irb(main):006:0> b
=> "4500"

1 Comment

You answer was been given by @Surya earlier.

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.