2

I have an array s_ary that contains all the lines of a text document. The last line may be something like return some_string.

When the last line starts with return, I want the line to become sv1 = some_string.

My original code does exactly what I need:

if s_ary.last =~ /^[\s]*return/
  t = s_ary.last.gsub(/^[\s]*return/, 'sv1 = ')
  s_ary.pop
  s_ary << t
else
  s_ary << 'sv1 = last'
end

I tried to improve it with:

if s_ary.last =~ /^[\s]*return/
  s_ary.map! {|x| (x =~/return/) ? x.gsub(/return/, 'sv1 = ') : x }
else
  s_ary << 'sv1 = last'
end

But this version will change all the lines that start with return when the last line starts with return. I could still use this last version as this is not supposed to happen in my application, but I have the feeling that there must a better, more compact way of accomplishing this. Somehow, I can't find it.

Thanks for any suggestion.

EDIT: The original code that does exactly what I need is actually (in agreement with the second paragraph of this post):

if s_ary.last =~ /^[\s]*return/
  t = s_ary.last.gsub(/^[\s]*return/, 'sv1 = ')
  s_ary.pop
  s_ary << t
end

I can't believe I wrote something so confusing. My apologies.

2
  • Can you give one sample string and the expected output? so that I can relate what you are trying to do Commented May 3, 2013 at 13:40
  • Typical example: the last line contains 'return v1 + v2'. It needs to be replaced with 'sv1 = v1 + v2' Commented May 3, 2013 at 13:46

2 Answers 2

6

Why not just:

s_ary[-1].gsub!(/^return /, 'sv1 = ')

EDIT

Looking closer at your code, maybe what you want is kind of:

unless s_ary[-1].gsub!(/^return /, 'sv1 = ')
  s_ary << 'sv1 = last'
end

The first snippet will only change the last line if it starts with return, doing nothing otherwise. The second snippet will change the last line if it starts with return, and will append sv1 = last otherwise.

Not sure which one you need.

Sign up to request clarification or add additional context in comments.

4 Comments

I think that some_string in return some_string may be other than last.
@undur_gongor you're right, sub may be better. Let's see if the OP has anything to say.
Excellent! Short and sweet. Seems to work perfectly. Thanks.
Regarding sub vs. gsub (actually I wasn't familiar with sub -- still a lot to learn), I also agree that sub is better in theory, as I want to replace only the first occurrence of 'return'. Even though I don't think I would ever find several 'return's in that last line. Thanks for pointing that out.
2
arr = ["Foo is good","Bar is bad","return v1 + v2"]
arr[-1] = arr.last.sub("return","sv = ") if arr.last.start_with? "return"
p arr #=> "Foo is good", "Bar is bad", "sv =  v1 + v2"]

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.