开发者

After passing a reference to an method, any mods using that reference are not visible outside the method - Ruby?

I am passing the reference of name to mod_name, I modify the referenced object from within the method but the change is not visible outside of the method, if I am referring to the same object from all locations how come the value is different depending on where I reference it?

name = "Jason"

puts name.object_id      #19827274

def mod_name(name)
  puts name.object_id    #19827274
 开发者_开发问答 name = "JasonB"
end

puts name.object_id      #19827274

puts name                #Jason

String might be a bad example, but I get the same result even if I use a Fixnum.


As Greg mentions, in your example you're creating a new local variable called name that is shadowing your parameter. This is due to behavior called copy-on-write. If you wanted the function to affect the object the parameter references, you could use replace instead of doing an assignment, like this:

def mod_name(name)
  name.replace('JasonB')
end


mutating the content of a string :

def mod_name(name)
  print "%i %s\n" % [name.object_id, name]
  name[0..-1] = "what"
  print "%i %s\n" % [name.object_id, name]
  name << "ever"
  print "%i %s\n" % [name.object_id, name]
end

Also :

  • in your sample code you never call the mod_name function.

  • You seemed to believe that writing 'var = x' would change var's content. It can't. It can only change what object gets pointed to by var. Asking for its object_id before and after affectation would have showed that it was indeed another string.

  • There's no way to make this function work with Fixnums because Fixnums are immutable in ruby. You can't make any modification to a Fixnum. Worse, they're not even passed by reference and in some corner cases won't behave like proper objects.


I think you have gotten answers to the question you asked. Maybe you need an answer to what would have caused you to ask this question.

name = "jason"

def mod_name(name)
  local_name = "jasonb"
end

puts name = mod_name(name)
puts name

Depending upon where this method lives (if it were in a class for example) maybe you would utilize an instance variable @name and depending upon the scope wouldn't have to be passed.

@name = "jason"

def mod_name()
  @name = "jasonb"
end

puts @name


In this case, the name = "JasonB" line is creating a new local variable named name and assigning "JasonB" to it, rather than modifying the string which was passed in.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜