Module: YAML

Defined in:
lib/standard/facets/yaml/file.rb,
lib/standard/facets/yaml/read.rb

Class Method Summary collapse

Class Method Details

.file?(file) ⇒ Boolean

Provides a simplistic way to check if a file is a YAML formatted file:

YAML.file?('project.yaml')  #=> true
YAML.file?('project.xml')   #=> false

Note this isn’t perfect. At present it depends on the use use of an initial document separator (eg. ‘—’). With YAML 1.1 the %YAML delaration is supposed to be manditory, so in the future this can be adapted to fit that standard.

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
# File 'lib/standard/facets/yaml/file.rb', line 16

def self.file?(file)
  File.open(file) do |f|
    until f.eof?
      line = f.gets
      break true if line =~ /^---/
      break false unless line =~ /^(\s*#.*?|\s*)$/
    end
  end
end

.read(file) ⇒ Object

Alias for ‘YAML.load_file` and the same as:

YAML.load(File.new(file))


7
8
9
# File 'lib/standard/facets/yaml/read.rb', line 7

def self.read(file)
  load(File.new(file))
end