简体   繁体   中英

How can I remove rocket notation from my ruby Hash?

First-time poster here...

Scenario

I'm looking into some existing logs being spat out by our Ruby on Rails applications and I am struggling to motivate for rocket notation, so I've come asking the community for some suggestions.

Here's an example of a log that one might come across in our ElasticSearch database (let's not get into that topic):

# reduced for the sake of this example
{
  message: "Completed Foo::Bar request to /api/v1/pictures with 200 OK in 64.121ms",
  schema: "http",
  library: "Foo::Bar",
  request: {
    :path=>"/api/v1/pictures",
    :query=>nil
  }
}

At first, I figured it was harmless, but then I started pairing with a wider group of developers (I work in a Platform Team) and I found that it wasn't immediately obvious how one could write a query on multiple key/value pairs when the request portion of the log was in rocket notation (Not looking for suggestions on this, it's just the scenario).

Question

So, my questions are:

  1. Is there a way to efficiently convert an entire Hash object into JSON-like symbol notation and purge all rockets ( => ) from the pl.net?

  2. [OPTIONAL] Should I be loving rocket notation more or are we in agreement that it should die? ( RIP Ruby Hash Rocket Syntax )

What I've tried

It seems to_json will give me what I want, but I am concerned that some vendors/ingestors might not enjoy the backslashes ( \ ) if I go for a more generic solution in my Rails application:

pry(main)> {
  message: "Completed Foo::Bar request to /api/v1/pictures with 200 OK in 64.121ms",
  schema: "http",
  library: "Foo::Bar",
  request: {
    :path=>"/api/v1/pictures",
    :query=>nil
  }
}.to_json
=> "{\"message\":\"Completed Foo::Bar request to /api/v1/pictures with 200 OK in 64.121ms\",\"schema\":\"http\",\"library\":\"Foo::Bar\",\"request\":{\"path\":\"/api/v1/pictures\",\"query\":null}}"

Expected Output

pry(main)> example_log = {
  message: "Completed Foo::Bar request to /api/v1/pictures with 200 OK in 64.121ms",
  schema: "http",
  library: "Foo::Bar",
  request: {
    :path=>"/api/v1/pictures",
    :query=>nil
  }
};
pry(main)> pp example_log.deep_purge_rockets;
=> {
  message: "Completed Foo::Bar request to /api/v1/pictures with 200 OK in 64.121ms",
  schema: "http",
  library:"Foo::Bar",
  request: {
    path: "/api/v1/pictures",
    query: ni
  }
}

Is there a way to efficiently convert an entire Hash object into JSON-like symbol notation and purge all rockets (=>) from the pl.net?

Hashrockets and colon style are merely two different syntaxes for writing hash literals and has no actual significance to the Hash object that is created.

The JSON style syntax is merely a more convenient and compact way of writing hashes with symbol keys and doesn't actually completely replace hashrockets which are still needed for dealing with hashes where the keys are not symbols.

The output from Hash#to_s for example will always use hashrockets no matter how you created the hash. Ruby doesn't actually have seperate classes for "symbol only" hashes and "mixed-type" hashes and needs to be able to represent both in a non-ambiguous way. If you don't like that then don't use implicit string conversion.

If you're talking about changing the code occurances of hashrockets there are tons of tools from simple grep oneliners to IDE plugins that can be used to convert legacy code.

[OPTIONAL] Should I be loving rocket notation more or are we in agreement that it should die? (RIP Ruby Hash Rocket Syntax)

The consensus in the Ruby community is pretty much in consensus that colon notation should be used when writing hashes with only symbol keys. Hashrockets are still very much necissary though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM