Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to print an object's class & contents in Rails

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 456
    Comment on it

    If i have a User object say user, i.e

    >> user = User.find(1)
    => #<User id: 1, login: "james.warner", email: "james.warner@evontech.com",created_at: "2013-11-18 07:12:45", updated_at: "2014-02-03 10:51:02", designation: "Programmer">
    

    Now if i want to see which class does this user object belongs to , i can write

    >> user.class
    => User(id: integer, login: string, email: string, created_at: datetime, updated_at: datetime, designation: string)
    

    It tell us that the user object belongs to the User class having the specified set of attributes.

    And to print the contents of this user object we have the following 2 methods :

    1) .inspect method

    >> puts user.inspect
    => #<User id: 1, login: "james.warner", email: "james.warner@evontech.com",created_at: "2013-11-18 07:12:45", updated_at: "2014-02-03 10:51:02", designation: "Programmer">
    

    2) .to_yaml

    >> puts user.to_yaml
    --- !ruby/object:User 
    attributes: 
      login: james.warner
      updated_at: 2014-02-03 10:51:02
      id: "1"
      designation: Programmer
      email: james.warner@evontech.com
      created_at: 2013-11-18 07:12:45
    attributes_cache: {}
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: