Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8836447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:29:49+00:00 2026-06-14T09:29:49+00:00

In ruby: class Obj attr_accessor :price end my_ruby_obj = Obj.new puts my_ruby_obj.to_json \#<Obj:0x1c9b018>\ Server

  • 0

In ruby:

class Obj
  attr_accessor :price
end

my_ruby_obj = Obj.new

puts my_ruby_obj.to_json

"\"#<Obj:0x1c9b018>\""

Server sends my_ruby_obj.to_json to browser (sinatra, with content_type :json) in response to a jquery ajax request (with $.get), which correctly defines a return type of json, and launches a callback function(data) which does:

console.log(data);

which returns:

#<Obj:0x1c9b018>

Now how do I access “data.price” attribute in javascript/jquery as I would normally do on a ruby object to access its attribute? (“my_ruby_obj.price”)

I tried with console.log(data.price);

> Undefined

I feel I am missing a big piece here.. and I guess it has to do with how json works with objects..

Any help ?
If possible, I’m looking for a correct way to do it with jquery.

Thanks

edit:
trying to understand what is going on, I tried this in the callback:

newdata = $.parseJSON(data);

but debug console halts on it showing:

> uncaught exception: Invalid JSON: #<Obj:0x1c9b018>

I did call .to_json on the ruby object, so why it says so? …

[[new_edit:]]

Seems like ruby json serialization for objects from custom classes is not working as I supposed:

Quoting from here: https://stackoverflow.com/a/4464721/988591

It will be a bit more difficult for objects from your own classes. For
the following class, to_json will produce something like
"\"#<A:0xb76e5728>\"". This probably isn’t desirable. To effectively
serialise your object as JSON, you should create your own to_json
method.

and examples are following…
But.. wow… isn’t there an easier way ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:29:50+00:00Added an answer on June 14, 2026 at 9:29 am

    Ok I found exactly what I was looking for! An automated quick solution, which also comes with other side benefits (more speed!).

    The standard json gem only works for some classes (Array, Hash,..) and can’t translate a custom class/object you have created to JSON. It doesn’t know how to organize data, unless you write yourself a custom .to_json method for your class, which is what I was tring to avoid..

    So in the end I found a gem called Oj: Optimized Json (gem install oj)

    Quoting the authors:

    Optimized JSON (Oj), as the name implies was written to provide speed
    optimized JSON handling. It was designed as a faster alternative to
    Yajl and other the common Ruby JSON parsers. So far is has achieved
    that at about 2 time faster than Yajl for parsing and 3 or more times
    faster writing JSON
    .

    Oj has several dump or serialization modes which control how Objects are converted to JSON. These modes are set with the :mode option in
    either the default options or as one of the options to the dump()
    method.

    :object mode will dump any Object as a JSON Object with keys that
    match the Ruby Object’s variable names without the ‘@’ character
    . This
    is the highest performance mode.

    I used it like so:

    json = Oj.dump(guy, mode: :object) but in my case it worked without mode: :object as well.

    now console.log(data); shows:

    ({'^o':"Human", x:38, y:58, name:"Jack", bday:{'^t':1352083824.5625}, age:50})
    

    Now that the serialization is working good, I can finally do:

    console.log(data.age); // —> 50

    Serialization is faster too.

    ..It has been a long ride though 😉

    A big Thank You goes to the library author: http://twitter.com/#!/peterohler

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: #!/usr/bin/ruby class Person def self.speak p = self.new puts
Consider the following sample ruby class class User def hello puts hello end end
So I create a class in ruby: class User def initialize end end Now
I wrote the following Ruby code file: require 'MyAssembly.dll' class MyClass def initialize(obj) System::Diagnostics::WriteLine('test')
Say I have a Ruby class, Flight . Flight has an attr_accessor :key on
When I try to execute: #!/usr/bin/env ruby class WrongNumberOfPlayersError < StandartError ; end class
I created ruby class and put it in app_dir/lib/appointment_messaging_job.rb class AppointmentMessagingJob def perform end
#!ruby class Car < ActiveRecord::Base belongs_to :user end @cars = Car.where(:user_id => current_user.id).limit(10) I
#!/usr/local/bin/ruby class OReport attr_accessor :id, :name def initialize(id, name, desc) @id = id @name
Foo = Class.new Foo.class_eval do def class_bar class_bar end end Foo.instance_eval do def instance_bar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.