简体   繁体   中英

Why don't numbers support .dup?

>> a = 5
=> 5
>> b = "hello, world!"
=> "hello, world!"
>> b.dup
=> "hello, world!"
>> a.dup
TypeError: can't dup Fixnum
    from (irb):4:in `dup'
    from (irb):4

I understand that Ruby will make a copy every time you assign an integer to a new variable, but why does Numeric#dup raise an error?

Wouldn't this break abstraction, since all objects should be expected to respond to .dup properly?

Rewriting the dup method will fix the problem, as far as I can tell:

>> class Numeric
>>   def dup()
>>     self
>>   end
>> end

Does this have a downside I'm not seeing? Why isn't this built into Ruby?

Most objects in Ruby are passed by reference and can be dupped. Eg:

s = "Hello"
t = s      # s & t reference to the same string
t.upcase!  # modifying either one will affect the other
s # ==> "HELLO"

A few objects in Ruby are immediate, though. They are passed by value, there can only be one of this value and it therefore cannot be duped. These are any (small) integers, true , false , symbols and nil . Many floats are also immediates in Ruby 2.0 on 64 bit systems.

In this (preposterous) example, any "42" will hold the same instance variable.

class Fixnum
  attr_accessor :name
  alias_method :original_to_s, :to_s
  def to_s
    name || original_to_s
  end
end
42.name = "The Answer"
puts *41..43  # =>  41, The Answer, 43

Since you would normally expect something.dup.name = "new name" to not affect any other object than the copy obtained with dup , Ruby chooses not to define dup on immediates.

Your question is more complex than it appears. There was some discussion on ruby-core as to how this can be made easier. Also, other types of Numeric objects (floats, bignums, rationals and complex numbers) can not be duped although they are not immediates either.

Note that ActiveSupport (part of rails) provide the method duplicable? on all objects

The problem with the dup() function that you defined is that it doesn't return a copy of the object, but rather returns the object itself. This is not what a duplicate procedure is supposed to do.

I don't know Ruby, but a possible reason I can think of for dup not being defined for numbers is that a number is a basic type and thus, doing something like:

>> a = 5
>> b = a

would automatically assign the value 5 into the variable b , as opposed to making b and a point to the same value in memory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM