When passing objects as parameters, are they passed by reference?

后端 未结 5 560
名媛妹妹
名媛妹妹 2021-01-12 12:01

Is there any performance implications if I do either of these:

def do_something(user, article)
...
end

versus

def do_someth         


        
5条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-12 12:19

    No, Ruby never passes by reference. Ruby is pass-by-value. Always. No exceptions.

    def is_Ruby_pass_by_value_or_reference?(parameter)
      parameter = 'Ruby is pass-by-reference.'
    end
    
    var = 'Ruby is pass-by-value. Always. No exceptions.'
    
    is_Ruby_pass_by_value_or_reference?(var)
    
    puts var
    # Ruby is pass-by-value. Always. No exceptions.
    

    If Ruby were pass-by-reference, this would print Ruby is pass-by-reference.

提交回复
热议问题