Using Gems in Your Code
To use a gem, you simply require
the gem in your code, which is similar to how you would include
a module. Consider the following example:
user = { name: 'John Smith', age: '35', address: { home: '1 kings cross road' }} puts JSON.pretty_generate(user) # NameError (uninitialized constant JSON) require 'json' puts JSON.pretty_generate(user) { Â Â "name": "John Smith", Â Â "age": "35", Â Â "address": { Â Â Â Â "home": "1 kings cross road" Â Â } }
In the preceding example, we create a simple hash containing some user information and we attempt to convert it to JSON and display it in a formatted way using the JSON.pretty_generate
function.
We can then see that it throws a NameError
error because the JSON
gem has not been required and is, therefore, not available. This is very much like trying to use...