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

  • SEARCH
  • Home
  • 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 7611077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:34:35+00:00 2026-05-31T01:34:35+00:00

#!/usr/bin/env ruby require ‘json’ class A def to_json(*a) { :a => ‘a’ }.to_json(*a) end

  • 0
#!/usr/bin/env ruby

require 'json'

class A
  def to_json(*a)
    { :a => 'a' }.to_json(*a)
  end
end

class B < A
  def to_json(*a)
    super({ :b => 'b' })
  end
end

puts B.new.to_json

produces

{"a":"a"}

How do I get it to produce

{"a":"a", "b":"b"}

in a reasonable way?

I’m using Ruby 1.9.3 and the latest json gem.

A related question is: what are the arguments *a to to_json? I’ve scoured the docs to no avail.

  • 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-05-31T01:34:36+00:00Added an answer on May 31, 2026 at 1:34 am

    You have two hashes {:a=>'a'} and {:b=>'b'} in two classes, they’re encapsulated i.e. hidden from outside world. The only way I can see is parse the json string into hash and merge them, then convert the result to json.

    class B < A
      def to_json(*a)
        JSON.parse(super).merge({:b=>'b'}).to_json
      end
    end
    

    But here will be small difference: you’re merging {:a=>'a',:b=>'b'} and got the {"a":"a","b":"b"}

    *a is parameter to set options for json format

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

Sidebar

Related Questions

Here is my script #!/usr/bin/env ruby if __FILE__ == $0 `cd ..` puts `ls`
I'm trying to understand the script presented on this site : #!/usr/bin/env ruby require
I've got a ruby script myscript.sh like this #!/usr/bin/env ruby puts 'do some ruby
I'm learning grit (version 2.4.1). Here is my basic code: #!/usr/bin/env ruby -wKU require
I have the following script: #!/usr/bin/env ruby require 'rubygems' require 'net/ssh' Net::SSH.start('host1', 'root', :password
I am getting this error when I run the following code: #!/usr/bin/env ruby -rubygems
My code: #!/usr/bin/env python def Runaaall(aaa): Objects9(1.0, 2.0) def Objects9(aaa1, aaa2): If aaa2 !=
I am trying to compare two Ruby Hashes using the following code: #!/usr/bin/env ruby
I've narrowed the problem down to the following simple code snippet: #!/usr/bin/env ruby print
!/usr/bin/env perl use warnings; use strict; my $text = 'hello ' x 30; printf

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.