2

I'm a newbie developing my second project in Ruby on Rails: A graphic calculator.

I'm using octave to evaluate the expressions. I'm already able to evaluate them and grab the output to a string, but it comes as "ans = 48", for example. I need to get rid of the "ans = " from the string, but I can't find anything about parsing strings that would be helpful for my case. Most of the articles are talking about HTML or URL parsing. In visual basic there were parsing functions like left(), mid() and right().

Is there any equivalent for ruby?

MK

3 Answers 3

1

If you are always parsing around an equals sign, and it's the only one in the expression, you can get the value on the right-hand-side of the expression like this:

expression = "ans = 48"
expression.split('=').last.strip
=> "48"
Sign up to request clarification or add additional context in comments.

Comments

0

Look at the methods on strings. There are a number, but the easiest might be to simply split the string on space -- or maybe something a little more complicated -- using String#slice

Comments

0

you can replace the ans = with an empty string using gsub

expression.gsub!('ans = ','')

p expression
=> '48'

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.