views:

107

answers:

2
>> 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?

+1  A: 

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.

cmptrgeekken
That's exactly what Ruby does, so when you use my method with a numeric type, it will return a new copy. (and for any other type, it will return a reference to the original)
Jeffrey Aylesworth
I suppose that is true. From a functionality standpoint, then, there probably isn't an issue with your code. However, the reason Ruby wouldn't have it built in is most likely because it would just be duplicating the built-in assignment functionality of Ruby's basic types.
cmptrgeekken
+3  A: 

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.

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 is ongoing 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

Marc-André Lafortune
ActiveSupport, not Rails, provides the `duplicable?` method. So you could just install ActiveSupport and require it (`require 'active_support'`) if you want that (and many other) utility methods.
dvyjones
Indeed. Updated.
Marc-André Lafortune