0

I would like to pass a variable by reference to a function. In this way, in this function the variable can be modified and the new value will be available also in the part of the program

e.g.

In soft_1.rb

 func=Remote::CheckPart.new()  # function defined in check.rb
 warning=""
 out=func.check(warning)
 puts "New Warning #{warning}!"

In check.rb

def check(warning)
    warning << "WARNING DUE TO SOME EVENT!"
end

1 Answer 1

3

You cannot pass by reference to ruby.

On the other hand, String in ruby is not immutable, so using << actually changes the string you are passing - so your code actually works as you wanted it to:

class Remote::CheckPart
  def check(warning)
    warning << "WARNING DUE TO SOME EVENT!"
  end
end

func=Remote::CheckPart.new()
warning=""
out=func.check(warning)
puts "New Warning #{warning}!"

will actually output:

New Warning WARNING DUE TO SOME EVENT!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.