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 849565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:07:25+00:00 2026-05-15T07:07:25+00:00

I was wondering if anyone could point out a cleaner better way to write

  • 0

I was wondering if anyone could point out a cleaner better way to write my code which is pasted here. The code scrapes some data from yelp and processes it into a json format. The reason I’m not using hash.to_json is because it throws some sort of stack error which I can only assume is due to the hash being too large (It’s not particularly large).

  • Response object = a hash
  • text = the output which saves to file

Anyways guidance appreciated.

def mineLocation

  client = Yelp::Client.new
  request = Yelp::Review::Request::GeoPoint.new(:latitude=>13.3125,:longitude => -6.2468,:yws_id => 'nicetry')
  response = client.search(request) 
  response['businesses'].length.times do |businessEntry|
    text =""
     response['businesses'][businessEntry].each { |key, value|
        if value.class == Array 
          value.length.times { |arrayEntry|
            text+= "\"#{key}\":["
             value[arrayEntry].each { |arrayKey,arrayValue|
              text+= "{\"#{arrayKey}\":\"#{arrayValue}\"},"
             }
             text+="]"   
          }
        else 
              text+="\"#{arrayKey}\":\"#{arrayValue}\"," 
        end
       }
  end
 end
  • 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-15T07:07:26+00:00Added an answer on May 15, 2026 at 7:07 am

    It looks like all your code is ultimately doing is this:

    require 'json'
    
    def mine_location
      client = Yelp::Client.new
      request = Yelp::Review::Request::GeoPoint.new(latitude: 13.3125,
        longitude: -6.2468, yws_id: 'nicetry')
      response = client.search(request)
    
      return response['businesses'].to_json
    end
    

    Which works just fine for me.

    If, for whatever reason you really must write your own implementation of a JSON emitter, here’s a couple of tips for you.

    The number 1 thing you completely ignore in your code is that Ruby is an object-oriented language, more specifically a class-based object-oriented language. This means that problems are solved by constructing a network of objects that communicate with each other via message passing and respond to those messages by executing methods defined in classes to which those objects belong.

    This gives us a lot of power: dynamic dispatch, polymorphism, encapsulation and a ton of others. Leveraging those, your JSON emitter would look something like this:

    class Object
      def to_json; to_s                                                         end
    end
    
    class NilClass
      def to_json; 'null'                                                       end
    end
    
    class String
      def to_json; %Q'"#{to_s}"'                                                end
    end
    
    class Array
      def to_json; "[#{map(&:to_json).join(', ')}]"                             end
    end
    
    class Hash
      def to_json; "{#{map {|k, v| "#{k.to_json}: #{v.to_json}" }.join(', ')}}" end
    end
    

    mine_location looks just like above, except obviously without the require 'json' part.

    If you want your JSON nicely formatted, you could try something like this:

    class Object
      def to_json(*) to_s    end
    end
    
    class String
      def to_json(*) inspect end
    end
    
    class Array
      def to_json(indent=0)
        "[\n#{'  ' * indent+=1}#{
          map {|el| el.to_json(indent) }.join(", \n#{'  ' * indent}")
        }\n#{'  ' * indent-=1}]"
      end
    end
    
    class Hash
      def to_json(indent=0)
        "{\n#{'  ' * indent+=1}#{
          map {|k, v|
            "#{k.to_json(indent)}: #{v.to_json(indent)}"
          }.join(", \n#{'  ' * indent}")
        }\n#{'  ' * indent-=1}}"
      end
    end
    

    There’s actually nothing Ruby-specific in this code. This is pretty much exactly what the solution would look like in any other class-based object-oriented language like Java, for example. It’s just object-oriented design 101.

    The only thing which is language-specific is how to “modify” classes and add methods to them. In Ruby or Python, you literally just modify the class. In C# and Visual Basic.NET, you would probably use extension methods, in Scala you would use implicit conversions and in Java maybe the Decorator Design Pattern.

    Another huge problem with your code is that you are trying to solve a problem which is obviously recursive without actually ever recursing. This just can’t work. The code you wrote is basically Fortran-57 code: procedural with no objects and no recursion. Even just moving one step up from Fortran to, say, Pascal, gives you a nice recursive procedural solution:

    def jsonify(o)
      case o
      when Hash
        "{#{o.map {|k, v| "#{jsonify(k)}: #{jsonify(v)}" }.join(', ')}}"
      when Array
        "[#{o.map(&method(:jsonify)).join(', ')}]"
      when String
        o.inspect
      when nil
        'null'
      else
        o.to_s
      end
    end
    

    Of course, you can play the same game with indentations here:

    def jsonify(o, indent=0)
      case o
      when Hash
        "{\n#{'  ' * indent+=1}#{
          o.map {|k, v|
            "#{jsonify(k, indent)}: #{jsonify(v, indent)}"
          }.join(", \n#{'  ' * indent}") }\n#{'  ' * indent-=1}}"
      when Array
        "[\n#{'  ' * indent+=1}#{
          o.map {|el| jsonify(el, indent) }.join(", \n#{'  ' * indent}") }\n#{'  ' * indent-=1}]"
      when String
        o.inspect
      when nil
        'null'
      else
        o.to_s
      end
    end
    

    Here’s the indented output of puts mine_location, produced using either the second (indented) version of to_json or the second version of jsonify, it doesn’t really matter, they both have the same output:

    [
      {
        "name": "Nickies",
        "mobile_url": "http://mobile.yelp.com/biz/yyqwqfgn1ZmbQYNbl7s5sQ",
        "city": "San Francisco",
        "address1": "466 Haight St",
        "zip": "94117",
        "latitude": 37.772201,
        "avg_rating": 4.0,
        "address2": "",
        "country_code": "US",
        "country": "USA",
        "address3": "",
        "photo_url_small": "http://static.px.yelp.com/bpthumb/mPNTiQm5HVqLLcUi8XrDiA/ss",
        "url": "http://yelp.com/biz/nickies-san-francisco",
        "photo_url": "http://static.px.yelp.com/bpthumb/mPNTiQm5HVqLLcUi8XrDiA/ms",
        "rating_img_url_small": "http://static.px.yelp.com/static/20070816/i/ico/stars/stars_small_4.png",
        "is_closed": false,
        "id": "yyqwqfgn1ZmbQYNbl7s5sQ",
        "nearby_url": "http://yelp.com/search?find_loc=466+Haight+St%2C+San+Francisco%2C+CA",
        "state_code": "CA",
        "reviews": [
          {
            "rating": 3,
            "user_photo_url_small": "http://static.px.yelp.com/upthumb/ZQDXkIwQmgfAcazw8OgK2g/ss",
            "url": "http://yelp.com/biz/yyqwqfgn1ZmbQYNbl7s5sQ#hrid:t-sisM24K9GvvYhr-9w1EQ",
            "user_url": "http://yelp.com/user_details?userid=XMeRHjiLhA9cv3BsSOazCA",
            "user_photo_url": "http://static.px.yelp.com/upthumb/ZQDXkIwQmgfAcazw8OgK2g/ms",
            "rating_img_url_small": "http://static.px.yelp.com/static/20070816/i/ico/stars/stars_small_3.png",
            "id": "t-sisM24K9GvvYhr-9w1EQ",
            "text_excerpt": "So I know gentrification is supposed to be a bad word and all (especially here in SF), but the Lower Haight might benefit a bit from it. At least, I like...",
            "user_name": "Trey F.",
            "mobile_uri": "http://mobile.yelp.com/biz/yyqwqfgn1ZmbQYNbl7s5sQ?srid=t-sisM24K9GvvYhr-9w1EQ",
            "rating_img_url": "http://static.px.yelp.com/static/20070816/i/ico/stars/stars_3.png"
          },
          {
            "rating": 4,
            "user_photo_url_small": "http://static.px.yelp.com/upthumb/Ghwoq23_alkaXawgqj7dBA/ss",
            "url": "http://yelp.com/biz/yyqwqfgn1ZmbQYNbl7s5sQ#hrid:8xTNOC9L5ZXwGCMNYY-pdQ",
            "user_url": "http://yelp.com/user_details?userid=4F2QG3adYIUNXplqqp9ylA",
            "user_photo_url": "http://static.px.yelp.com/upthumb/Ghwoq23_alkaXawgqj7dBA/ms",
            "rating_img_url_small": "http://static.px.yelp.com/static/20070816/i/ico/stars/stars_small_4.png",
            "id": "8xTNOC9L5ZXwGCMNYY-pdQ",
            "text_excerpt": "This place was definitely a great place to chill. The atmosphere is very non-threatening and very neighborly. I thought it was cool that they had a girl dj...",
            "user_name": "Jessy M.",
            "mobile_uri": "http://mobile.yelp.com/biz/yyqwqfgn1ZmbQYNbl7s5sQ?srid=8xTNOC9L5ZXwGCMNYY-pdQ",
            "rating_img_url": "http://static.px.yelp.com/static/20070816/i/ico/stars/stars_4.png"
          },
          {
            "rating": 5,
            "user_photo_url_small": "http://static.px.yelp.com/upthumb/q0POOE3vv2LzNg1qN8MMyw/ss",
            "url": "http://yelp.com/biz/yyqwqfgn1ZmbQYNbl7s5sQ#hrid:pp33WfN_FoKlQKJ-38j_Ag",
            "user_url": "http://yelp.com/user_details?userid=FmcKafW272uSWXbUF2rslA",
            "user_photo_url": "http://static.px.yelp.com/upthumb/q0POOE3vv2LzNg1qN8MMyw/ms",
            "rating_img_url_small": "http://static.px.yelp.com/static/20070816/i/ico/stars/stars_small_5.png",
            "id": "pp33WfN_FoKlQKJ-38j_Ag",
            "text_excerpt": "Love this place!  I've been here twice now and each time has been a great experience.  The bartender is so nice.  When we had questions about the drinks he...",
            "user_name": "Scott M.",
            "mobile_uri": "http://mobile.yelp.com/biz/yyqwqfgn1ZmbQYNbl7s5sQ?srid=pp33WfN_FoKlQKJ-38j_Ag",
            "rating_img_url": "http://static.px.yelp.com/static/20070816/i/ico/stars/stars_5.png"
          }
        ],
        "phone": "4152550300",
        "neighborhoods": [
          {
            "name": "Hayes Valley",
            "url": "http://yelp.com/search?find_loc=Hayes+Valley%2C+San+Francisco%2C+CA"
          }
        ],
        "rating_img_url": "http://static.px.yelp.com/static/20070816/i/ico/stars/stars_4.png",
        "longitude": -122.429926,
        "categories": [
          {
            "name": "Dance Clubs",
            "category_filter": "danceclubs",
            "search_url": "http://yelp.com/search?find_loc=466+Haight+St%2C+San+Francisco%2C+CA&cflt=danceclubs"
          },
          {
            "name": "Lounges",
            "category_filter": "lounges",
            "search_url": "http://yelp.com/search?find_loc=466+Haight+St%2C+San+Francisco%2C+CA&cflt=lounges"
          },
          {
            "name": "American (Traditional)",
            "category_filter": "tradamerican",
            "search_url": "http://yelp.com/search?find_loc=466+Haight+St%2C+San+Francisco%2C+CA&cflt=tradamerican"
          }
        ],
        "state": "CA",
        "review_count": 32,
        "distance": 1.87804019451141
      }
    ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering if anyone could help me out, or point me in the
I was wondering if anyone could point out why I'm not able to capture
I was wondering if anyone could give me an example or point me to
I'm wondering if anyone can help me out with some Canvas. I'm pretty new
I was wondering if I could get some help on a proper way of
I was wondering if anyone could point me to any resources or overviews as
I have some code which pulls records out of the database and I'm then
So I was wondering if anyone knew how, or could point me in the
I was wondering if anyone could point to an Open Source date utility class
I'm pretty new to lisp; I was wondering if anyone here could help me

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.