4
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

YAML内でYAMLをincludeする(render :partialみたいな感じ)

Last updated at Posted at 2015-12-11
# YAMLファイルを結合(include)したり、Hash化(load)したりする
#
# 使い方)
# aaa.yml -------
#   - id: <%= id %>
#     aaa: 'aaaa'
#
# bbb.yml -------
#   - id: <%= id %>
#     bbb: 'bbbb'
#
# result.yml ------
#   <%= Util::Yaml.include('aaa.yml', { id: 1 }) %>
#   <%= Util::Yaml.include('bbb.yml', { id: 2 }) %>
#
module Util
  class Yaml
    def self.include(file_path, locals = {})
      Erubis::Eruby.new(IO.read(file_path)).result(locals)
    end

    def self.load(file_path, locals = {})
      YAML::load(include(file_path, locals))
    end
  end
end

4
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?