0

I have ruby script as follows:

class Person
    def initialize(name)
        @name = name
    end

    def aMethod()
        puts "Excecuting aMethod"
    end
end

class Employee < Person
end

e1 = Employee.new("Salunke")
e1.id

after the execution of above script im getting following error:

first.rb:16: warning: Object#id will be deprecated; use Object#object_id

how to resolve above warning/error?

0

3 Answers 3

3

According to the warning:

Object#id will be deprecated; use Object#object_id

so, replace the id with object_id

e1.object_id
Sign up to request clarification or add additional context in comments.

5 Comments

thanks for suggestion, it gives following error "first.rb:16: undefined method `obejct_id' for #<Employee:0x2acdf9c95da0 @name="Salunke"> (NoMethodError)"
@prajkata I probably misunderstood the situation here. One question, is this pure Ruby or Ruby on Rails?
its a pure ruby. And can u tell me that is there any property or method defined in ruby named as "id". Thanks for ans.
@praj, in my Ruby 1.9.2p180, there is no such warning. Only the error undefined method 'id'.. the warning you are getting looks like from rails. anyway, you probably need to declare your own id inside your class definition.
@prajakata...u have a typo in calling object_id if that is what u want...it should be object_id in place of obejct_id
0

In your example, "e1" is an instance of your class "Employee" (which extend Person)

When you invoke the method "id" (send the message "id" to the object) on the instance "e1", ruby try to call the id method on the whole chain of objects.

The main Object class didn't declare an "id" but an "object_id" method.

As Phelios pointed out, the message is clear enough.

Comments

0

Problem is: There is an id method on class Object, but thats probably not the kind of id you want. It's the object-id of your employee instance generated by the ruby vm . Since thats a pretty common mistake, the id method got deprecated in favour of the more concise object_id. You would have to implement an id method yourself, or, if you really want the object_id, call that method instead of calling id

2 Comments

thanks very much. can u tell me details about the "id" method on class Object. I want that only.
If you really want the ruby-vm's object-id you just call object_id instead of id. That will stop the warning you are seeing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.