Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby hash hardcoding programming best practices

I'm reading about ruby programming best practices and I found this page: https://github.com/styleguide/ruby

Inside this, a "Hashes" section explain that:

# bad
user = {
  login: "defunkt",
  name: "Chris Wanstrath"
}

# bad
user = {
  login: "defunkt",
  name: "Chris Wanstrath",
  "followers-count" => 52390235
}

# good
user = {
  :login => "defunkt",
  :name => "Chris Wanstrath",
  "followers-count" => 52390235
}

Why the first block are marked as bad if I can read better than the last block?

like image 258
Javier Valencia Avatar asked Jul 01 '26 01:07

Javier Valencia


1 Answers

It's just a style guide GitHub uses for its code base, nothing more. They probably prefer it because a lot of their code had been written before Ruby 1.9 was established and they want to keep things consistent.

I personally use the new syntax everywhere I can. Note that the last example is bad on its own because it mixes symbol and string keys together.

like image 116
Jiří Pospíšil Avatar answered Jul 02 '26 15:07

Jiří Pospíšil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!