0

Try to do this as a one-liner, a little confused why this doesn't work:

var= balh.getArrayOStrings('sdfsfd').each{ it.replaceAll("herp","derp") }

I call a method that returns an array of string. I loop over them and do a replace and assign back to var, but the modified strings in the array are not assigned back to var. Why not?

1 Answer 1

1

each returns the original list, try

balh = [:]
balh.getArrayOStrings = { x ->
    [ "sudhpa Herp", "pskaap herp", "herp lsjkda"]
}

var = balh.getArrayOStrings('sdfsfd').collect { it.replaceAll("herp","derp") }

=> [sudhpa Herp, pskaap derp, derp lsjkda]

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

1 Comment

Or just balh.getArrayOStrings('sdfsfd')*.replaceAll("herp","derp") for short by using the spread operator.

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.