Skip to content

Allow Hash#smash to include the options#1

Open
Jakanapes wants to merge 3 commits intol3x:masterfrom
Jakanapes:master
Open

Allow Hash#smash to include the options#1
Jakanapes wants to merge 3 commits intol3x:masterfrom
Jakanapes:master

Conversation

@Jakanapes
Copy link
Copy Markdown

it would be nice to be able to call hash.smash(smash_array: true) instead of having to do Flatten.smash(hash, smash_array: true)

@l3x
Copy link
Copy Markdown
Owner

l3x commented Apr 2, 2019

Thank you for your interest in flatten! I hope you find it useful.

Here're my thoughts on this PR:

The readme suggests calling flatten this way:

my_house.smash
{
    "dh-description" => "my house in dunwoody",
         "dh-street" => "5252 vernon lake drive",
           "dh-city" => "atlanta",
          "dh-state" => "GA",
              "name" => "lex"
}

If we were to merge your changes, it seems we'd by saying the following is preferable:

my_house.smash(smash_array: true)
{
    "dh-description" => "my house in dunwoody",
         "dh-street" => "5252 vernon lake drive",
           "dh-city" => "atlanta",
          "dh-state" => "GA",
              "name" => "lex"
}

Isn't the first way simpler? What's the benefit of providing a longer way to do the same thing?

Best, Lex

close parens
@Jakanapes
Copy link
Copy Markdown
Author

But it's not the same thing. I'd like to be able to specify that it smash arrays and add a separator.

h = { "build" => { "year" => 1925, "color" => "red" }, "images" => ["url1", "url2"] }

[7] pry(main)> h.smash
=> {"year"=>1925, "color"=>"red", "images"=>["url1", "url2"]}
[8] pry(main)> h.smash(smash_array: true, separator: '/')
=> {"build/year"=>1925, "build/color"=>"red", "images/0"=>"url1", "images/1"=>"url2"}

Basically, I want the same output as this

[9] pry(main)> Flatten.smash(h, smash_array: true, separator: '/')
=> {"build/year"=>1925, "build/color"=>"red", "images/0"=>"url1", "images/1"=>"url2"}

It's not terrible to call the class method, but it would be convenient to be able to call it from an instantiated hash object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants