How to Use Yaml in Ruby/Rails

How do I use YAML in ruby/rails?

Here, you save your yaml objects as Person objects and then when you load them back, they will load into Person objects, making them a lot easier to handle.

First change tweak your yaml file to something like this:

--- 
- !ruby/object:Person
name: John Doe
sname: jdoe
email: jdoe@gmail.com
- !ruby/object:Person
name: Jane Doe
sname: jdoe
email: jane@hotmail.com

Now you can load your yaml file into an array of Person objects and then manipulate the array:

FILENAME = 'data.yaml'

class Person
attr_accessor :name, :sname, :email
end

require "yaml"
# Will return an array of Person objects.
data = YAML::load(File.open(FILENAME))

# Will print out the first object in the array's name. #=> John Doe
puts data.first.name

Rubygem: How to load information of a yaml file

You should be able to load app yaml files by doing this in your gem:

YAML.load_file('config/my_gem.yml')

This requires that the working directory is the root of your Rails application – which is the case when you invoke the rails command while in that directory.

Rails use of yml file

Setting Rails environment variables. Using ENV variables in Rails, locally and with Heroku. Rails configuration and security with environment variables.

Environment Variables

Many applications require configuration of settings such as email account credentials or API keys for external services. You can pass local configuration settings to an application using environment variables.

Operating systems (Linux, Mac OS X, Windows) provide mechanisms to set local environment variables, as does Heroku and other deployment platforms. Here we show how to set local environment variables in the Unix shell. We also show two alternatives to set environment variables in your application without the Unix shell.

Gmail Example

config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "example.com",
authentication: "plain",
enable_starttls_auto: true,
user_name: ENV["GMAIL_USERNAME"],
password: ENV["GMAIL_PASSWORD"]
}

You could “hardcode” your Gmail username and password into the file but that would expose it to everyone who has access to your git repository. Instead use the Ruby variable ENV["GMAIL_USERNAME"] to obtain an environment variable. The variable can be used anywhere in a Rails application. Ruby will replace ENV["GMAIL_USERNAME"] with an environment variable.

Option One: Set Unix Environment Variables

export GMAIL_USERNAME="myname@gmail.com"

Option Two: Use the Figaro Gem

  • This gives you the convenience of using the same variables in code
    whether they are set by the Unix shell or the figaro gem’s
    config/application.yml. Variables in the config/application.yml file
    will override environment variables set in the Unix shell.
  • Use this syntax for setting different credentials in development,
    test, or production environments:

**

HELLO: world
development:
HELLO: developers
production:
HELLO: users

**

In this case, ENV["HELLO"] will produce “developers” in development, “users” in production and “world” otherwise.

Option Three: Use a local_env.yml File

Create a file config/local_env.yml:

Hope this help your answer!!!

Ruby on Rails: Can you put Ruby code in a YAML config file?

Not normally / directly. I say this because in order to use ruby results you need to use something like ERB first before loading the file. In terms of code, you need to go from something like:

loaded_data = YAML.load_file("my-file.yml")

Or even

loaded_data = YAML.load(File.read("my-file.yml"))

To:

loaded_data = YAML.load(ERB.new(File.read("my-file.yml")).result)

In this specific case, you would have to look at what is loading the file - in some cases,
it may already be designed to load it straight out of the environment or you may need to either:

  1. Monkey Patch the code
  2. Fork + Use your custom version.

Since there are a few plugins that use amazon_s3.yml, It might be worth posting which library you are using that uses it - alternatively, I believe from a quick google that the AWS library lets you define AMAZON_ACCESS_KEY_ID and AMAZON_SECRET_ACCESS_KEY as env vars and it will pick them up out of the box.

What's a good approach for loading a YAML config in Ruby but setting default values?

You should prepare a default hash, and then overwrite the values from the configure by merging the hash.

default_app_config = {
"app_name" => "Default name",
"app_path" => "default/path",
...,
}

app_config = default_app_config.merge(YAML.load_config("config.yml"))

Rails 4 - Yaml configuration file

If you are using Rails 4.2 or higher, you can use config_for for the config files. They need to be placed under /config folder. (haven't tried otherwise)

In your case it would be: config = Rails.application.config_for(:application)

This is more clear and Rails way to load configs into application.

Then you can use OpenStruct to have dot notation enabled for it.

APP_CONFIG = OpenStruct.new(config)

How do I parse a YAML file in Ruby?

Maybe I'm missing something, but why try to parse the file? Why not just load the YAML and examine the object(s) that result?

If your sample YAML is in some.yml, then this:

require 'yaml'
thing = YAML.load_file('some.yml')
puts thing.inspect

gives me

{"javascripts"=>[{"fo_global"=>["lazyload-min", "holla-min"]}]}


Related Topics



Leave a reply



Submit