Forum: Ruby on Rails Object#id will be deprecated?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Joe (Guest)
on 2006-02-28 05:02
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?

Thanks,
Joe
Marcel Molina Jr. (Guest)
on 2006-02-28 05:30
(Received via mailing list)
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
Joe (Guest)
on 2006-02-28 05:40
Thanks for enlightening me, Marcel. :)

The code that sets @current_member is:
  @current_member = Member.find(:first, :conditions => '...')

So my guess is that a member wasn't found, presumably false was
returned, and then 'false.id' was called. Hmm, I'll have to rethink this
code...

Joe
Marcel Molina Jr. (Guest)
on 2006-02-28 05:45
(Received via mailing list)
On Tue, Feb 28, 2006 at 04:40:54AM +0100, Joe wrote:
> The code that sets @current_member is:
>   @current_member = Member.find(:first, :conditions => '...')
>
> So my guess is that a member wasn't found, presumably false was
> returned, and then 'false.id' was called. Hmm, I'll have to rethink this
> code...

If find(:first) doesn't find a result it returns nil. So if you are in
development mode you should be getting an exception raised. If you are
in
production mode you won't get an exception.

marcel
This topic is locked and can not be replied to.