Tag Archives: hash

Ruby Gems Tip: Check Your Version

Quick ruby gems tip:

If you are having issues with a gem (and it seems like the issue has been resolved in the github repo), double check your gem version.

Even though I had the latest version of a gem by having [cci]gem ‘leaflet-rails'[/cci] in my Gemfile, the latest version wasn’t bumped up at http://rubygems.org/ (Note: Nothing against leaflet-rails, which is awesome. They’re simply used as a recent, real-life example. )

You can make sure you’re using the latest version of a gem (as it appears on github) by specifying the repo & commit hash:

[cc]gem ‘leaflet-rails’, git: ‘git@github.com:axyjo/leaflet-rails.git’, ref: ‘0f50faaa35d41e8ba24c73c97d265e061b159d81′[/cc]

This will lock the specific commit in place in your Gemfile.lock

Convert a Ruby Hash into valid JSON

This post is about how to get a valid JSON (JavaScript Object Notation) snippet from a Ruby Hash.

In the world of Ruby, Ruby loves hashes. It’s great because it allows you to access values with keys. With [cci]require ‘json'[/cci], you can easily parse a JSON formatted string into a Ruby hash with [cci]JSON.parse(json_string)[/cci]. You can easily tell hashes by their hash rockets ([cci]=>[/cci]) and JSON by its usage of colons ([cci]:[/cci]).

[cc]
require ‘json’
animal_count = {“tiger”=>3, “lion”=>2, “dog”=>2, “cat”=>3, “mouse”=>1, “bear”=>1, “frog”=>2, “fish”=>1}
animal_count.class #=> Hash
animal_count.methods #=> :to_json
[/cc]

By using [cci]require ‘json'[/cci], you get access to the [cci]to_json[/cci] method on hashes. Unfortunately, they look like this:

[cc]
json_animals = animal_count.to_json #=> “{\”tiger\”:3, \”lion\”:2, \”dog\”:2, \”cat\”:3, \”mouse\”:1, \”bear\”:1, \”frog\”:2, \”fish\”:1}”
json_animals.class #=> String
[/cc]

If you paste the string [cci]”{\”tiger\”:3,\”lion\”:2,\”dog\”:2,\”cat\”:3,[/cci]
[cci]\”mouse\”:1,\”bear\”:1,\”frog\”:2,\”fish\”:1}”[/cci] into JSON Lint, you’ll find that it’s invalid JSON due to a Parse Error.

The backslash character ([cci]\[/cci]) is there to escape the quotes for serialization (Ruby object to transportable string) and deserialization (string to Ruby object).

The good news is that the solution in IRB is extremely easy.

[cc]
puts json_animals #=> {“tiger”:3,”lion”:2,”dog”:2,”cat”:3,”mouse”:1,”bear”:1,”frog”:2,”fish”:1}
[/cc]

With [cci]puts[/cci], the new return String passes JSON Lint as Valid JSON.

Using [cci]puts[/cci] may seem obvious, but it’s the seemingly small things like this that make all the difference in the world. One is properly escaped but invalid, while the other is valid, usable JSON.

(Thanks to Avi for helping me with this @Flatironschool)

How to count Ruby Array items

While working in Ruby, I often find myself trying to get a count of the number of times each item appears in an array. Below, I’ll show you how to create a Hash that iterates through each item in the array and counts the number of occurrences.

We have an array of [cci]animals[/cci] that we want to count.

[cc]animals = [“tiger”,
“tiger”,
“lion”,
“dog”,
“cat”,
“mouse”,
“bear”,
“frog”,
“tiger”,
“lion”,
“frog”,
“fish”,
“cat”,
“dog”,
“cat”
][/cc]
We can count them up by creating a new Hash.

[cc]animal_count = Hash.new(0)[/cc]

Next, we want to iterate through the [cci]animals[/cci] Array and set the [cci]animal_count[/cci] Key as the animal name & the [cci]animal_count[/cci] Value as the counter.

[cc]animals.each { |animal| animal_count[animal] += 1 }[/cc]

For each [cci]animal[/cci] in [cci]animals[/cci], we’ve either created a new Key / Value pair in [cci]animal_count[/cci] or incremented the Value corresponding to the [cci]animal[/cci] Key.
[cc]puts animal_count
#=> {“tiger”=>3, “lion”=>2, “dog”=>2, “cat”=>3, “mouse”=>1, “bear”=>1, “frog”=>2, “fish”=>1} [/cc]