This question already has answers here:
What's the difference between Ruby's dup and clone methods?

(6个答案)


已关闭6年。




我需要知道Rails dup和clone方法之间的区别,因为dup复制了id属性,而clone没有:
juan:~/alhambra$ rails c
Loading development environment (Rails 3.0.1)
1.9.3-p551 :001 > @user=User.last
 => #<User id: 2, email: "[email protected]", encrypted_password: "$2a$10$/Fsz8DZ9PQbReTU1.wyxS.m3IOxZSV3siKDrrtUJdupz...", password_salt: "$2a$10$/Fsz8DZ9PQbReTU1.wyxS.", reset_password_token: nil, remember_token: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2015-05-06 23:34:20", last_sign_in_at: "2015-05-06 23:34:20", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2015-05-06 23:33:37", updated_at: "2015-05-06 23:34:20">
1.9.3-p551 :002 > @user.clone
 => #<User id: nil, email: "[email protected]", encrypted_password: "$2a$10$/Fsz8DZ9PQbReTU1.wyxS.m3IOxZSV3siKDrrtUJdupz...", password_salt: "$2a$10$/Fsz8DZ9PQbReTU1.wyxS.", reset_password_token: nil, remember_token: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2015-05-06 23:34:20", last_sign_in_at: "2015-05-06 23:34:20", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2015-05-06 23:33:37", updated_at: "2015-05-06 23:34:20">
1.9.3-p551 :003 > @user.dup
 => #<User id: 2, email: "[email protected]", encrypted_password: "$2a$10$/Fsz8DZ9PQbReTU1.wyxS.m3IOxZSV3siKDrrtUJdupz...", password_salt: "$2a$10$/Fsz8DZ9PQbReTU1.wyxS.", reset_password_token: nil, remember_token: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2015-05-06 23:34:20", last_sign_in_at: "2015-05-06 23:34:20", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2015-05-06 23:33:37", updated_at: "2015-05-06 23:34:20">

最佳答案

在Rails 3.0中,dupclone扮演的角色与现在的角色基本相反。从ActiveRecord::Base:



While it can be seen in the same file表示,dup简单地复制了记录及其属性:

  def dup
    obj = super
    obj.instance_variable_set('@attributes', @attributes.dup)
    obj
  end

这与当前的rails 4不同,后者定义了dupclone,以更多地遵循ruby docs中注明的similar question not specific to rails的注释。



the more current ActiveRecord source可以看出:
##
# :method: clone
# Identical to Ruby's clone method.  This is a "shallow" copy.  Be
# warned that your attributes are not copied. That means that modifying
# attributes of the clone will modify the original, since they will both
# point to the same attributes hash. If you need a copy of your attributes
# hash, please use the #dup method.
#
#   user = User.first
#   new_user = user.clone
#   user.name               # => "Bob"
#   new_user.name = "Joe"
#   user.name               # => "Joe"
#
#   user.object_id == new_user.object_id            # => false
#   user.name.object_id == new_user.name.object_id  # => true
#
#   user.name.object_id == user.dup.name.object_id  # => false

##
# :method: dup
# Duped objects have no id assigned and are treated as new records. Note
# that this is a "shallow" copy as it copies the object's attributes
# only, not its associations. The extent of a "deep" copy is application
# specific and is therefore left to the application to implement according
# to its need.
# The dup method does not preserve the timestamps (created|updated)_(at|on).

关于ruby-on-rails - Rails dup和clone方法有什么区别?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30090176/

10-11 03:02