Parsing console output / Ruby on rails
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
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"
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
you can replace the ans = with an empty string using gsub
expression.gsub!('ans = ','')
p expression
=> '48'
精彩评论