[Rails] Object#id will be deprecated?

Marcel Molina Jr.
Tue, 28 Feb 2006 03:28:22 -0000

On Tue, Feb 28, 2006 at 04:02:47AM +0100, Joe wrote:
> I didn''t get the memo on this :P
>
> members_controller.rb:12: warning: Object#id will be deprecated; use
> Object#object_id
>
> Line 12 is:
> @member = Member.find(@current_member.id)
>
> How _should_ I be writing that line?

Object#id is a Ruby method for the internal id of the given object. It is a
method on *every* object that exists in the execution of your program. That
method is deprecated in favor of Object#object_id.

-------------------------------------------------------------- Object#id
    obj.id    => fixnum
------------------------------------------------------------------------
    Soon-to-be deprecated version of +Object#object_id+.

For example:

  >> ''some string''.object_id
  => 2613644
  >> %w(an array).object_id
  => 2611354
  >> Time.now.object_id
  => 2608914
  >> Object.new.object_id
  => 2606434
  >> false.object_id
  => 0
  >> nil.object_id
  => 4
  >> 25.object_id
  => 51

------------------------------------------------------- Object#object_id
     obj.__id__       => fixnum
     obj.object_id    => fixnum
------------------------------------------------------------------------
     Returns an integer identifier for _obj_. The same number will be
     returned on all calls to +id+ for a given object, and no two active
     objects will share an id. +Object#object_id+ is a different concept
     from the +:name+ notation, which returns the symbol id of +name+.
     Replaces the deprecated +Object#id+.

This is not to be confused with ActiveRecord::Base#id, which is the primary
key id for a record in your database for a given model. In your case
@current_member is, according to your expectation, a Member instance
when in actuality it is some other object. In the future, once/if Ruby
gets rid of Object#id entirely, you would get a NoMethodError exception here.
For now, if you get that warning, it means you are calling id on something
that is not an active record. In the development environment, if you call id
on nil a WhinyNil exception is raised, because nil.object_id is 4 which could
lead to a tricky bug to track down. Any other non-nil/non-active-record
object you call the id method on will not raise an exception, but
you''ll get
that warning from Ruby warning you that Object#id is deprecated.

marcel
--
Marcel Molina Jr. <marcel@vernix.org>