Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Is Ruby pass by reference or by value?

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 219
    Comment on it

    When we have to call a function with parameters there are basically two most generally known and easy to understand approaches for passing of parameters among all programming languages and these are pass-by-value and pass-by-reference. Ruby is pass-by-value, that is the direct answer. similar what java does.

    Lets take a simple example:

    def test(str)
      str = 'reference'
    end
    
    str1 = 'value'
    
    test(str1)
    
    puts "Ruby is pass-by-#{str1}"
    

    Method arguments are passed by value rather than by reference, but the values passed are object reference.

    Because object reference are passed to method, method can use those reference to convert the basic object. These conversions are visible when the method returns.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: