Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ruby 1.9 hash symbol syntax #16

Open
Paxa opened this issue Aug 7, 2023 · 0 comments
Open

Ruby 1.9 hash symbol syntax #16

Paxa opened this issue Aug 7, 2023 · 0 comments

Comments

@Paxa
Copy link

Paxa commented Aug 7, 2023

New hash syntax was introduced in ruby 1.9 around 2009 (14 years ago?)

this patch will use new syntax when possible and add spaces around =>

What do you think about making this as default behavior?

module PP::PPMethods

  def pp_hash(obj)
    group(1, '{', '}') {
      seplist(obj, nil, :each_pair) {|key, v|
        group {
          if key.is_a?(Symbol)
            if key.to_s =~ /^[\w_][\w\d_]+$/i
              text "#{key}: "
            else
              text "'#{key}': "
            end
          else
            pp key
            text ' => '
          end
          group(1) {
            breakable ''
            pp v
          }
        }
      }
    }
  end

end

# example:

pp(
  "string key" => 1111,
  "33 symbol .": 222,
  symbol: 333,
  sub: {
    hash: {
      _aaa_aaaa_111: 444,
      something_else: "eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJhdWQiOlsiZ29qZWsiLCJtaWR0cmFucyIsImdvdml",
      "and" => "eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJhdWQiOlsiZ29qZWsiLCJtaWR0cmFucyIsImdvdml"
    }
  }
)

# prints

{"string key" => 1111,
 '33 symbol .': 222,
 symbol: 333,
 sub: 
  {hash: 
    {_aaa_aaaa_111: 444,
     something_else: "eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJhdWQiOlsiZ29qZWsiLCJtaWR0cmFucyIsImdvdml",
     "and" => "eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJhdWQiOlsiZ29qZWsiLCJtaWR0cmFucyIsImdvdml"}}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant