How to find out all classes of ruby corelib? - ruby

I need to return an Array of all classes that are part of ruby core.
http://ruby-doc.org/core-2.2.3/
For instance, the array should be like this:
[Array, String, Hash etc..]
Can also be strings instead such as:
['Array', 'String', 'Hash']
This should lateron be used for looking up documentation of ri, but prior to invoke ri - and it must only work for ruby core docu, deliberately not so for any other part of ruby (vanilla ruby is to be targeted for one project here, and the only constant to be usable is the default ruby install on the user's computer).
Thanks.

ObjectSpace.each_object(Class). # Get all instances of `Class`
reject {|klass| klass.name.nil? } # Reject singleton classes
# => [ARGF.class, ArgumentError, Array, BasicObject, Bignum, Binding, Class,
# Complex, Complex::compatible, Data, Dir, EOFError, Encoding,
# Encoding::CompatibilityError, Encoding::Converter,
# Encoding::ConverterNotFoundError, Encoding::InvalidByteSequenceError,
# Encoding::UndefinedConversionError, EncodingError, Enumerator,
# Enumerator::Generator, Enumerator::Lazy, Enumerator::Yielder, Errno::E2BIG,
# Errno::EACCES, Errno::EADDRINUSE, Errno::EADDRNOTAVAIL, Errno::EAFNOSUPPORT,
# Errno::EAGAIN, Errno::EALREADY, Errno::EAUTH, Errno::EBADF, Errno::EBADMSG,
# Errno::EBADRPC, Errno::EBUSY, Errno::ECANCELED, Errno::ECHILD,
# Errno::ECONNABORTED, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::EDEADLK,
# Errno::EDESTADDRREQ, Errno::EDOM, Errno::EDQUOT, Errno::EEXIST,
# Errno::EFAULT, Errno::EFBIG, Errno::EFTYPE, Errno::EHOSTDOWN,
# Errno::EHOSTUNREACH, Errno::EIDRM, Errno::EILSEQ, Errno::EINPROGRESS,
# Errno::EINTR, Errno::EINVAL, Errno::EIO, Errno::EISCONN, Errno::EISDIR,
# Errno::ELOOP, Errno::EMFILE, Errno::EMLINK, Errno::EMSGSIZE,
# Errno::EMULTIHOP, Errno::ENAMETOOLONG, Errno::ENEEDAUTH, Errno::ENETDOWN,
# Errno::ENETRESET, Errno::ENETUNREACH, Errno::ENFILE, Errno::ENOATTR,
# Errno::ENOBUFS, Errno::ENODATA, Errno::ENODEV, Errno::ENOENT,
# Errno::ENOEXEC, Errno::ENOLCK, Errno::ENOLINK, Errno::ENOMEM, Errno::ENOMSG,
# Errno::ENOPROTOOPT, Errno::ENOSPC, Errno::ENOSR, Errno::ENOSTR,
# Errno::ENOSYS, Errno::ENOTBLK, Errno::ENOTCONN, Errno::ENOTDIR,
# Errno::ENOTEMPTY, Errno::ENOTRECOVERABLE, Errno::ENOTSOCK, Errno::ENOTSUP,
# Errno::ENOTTY, Errno::ENXIO, Errno::EOPNOTSUPP, Errno::EOVERFLOW,
# Errno::EOWNERDEAD, Errno::EPERM, Errno::EPFNOSUPPORT, Errno::EPIPE,
# Errno::EPROCLIM, Errno::EPROCUNAVAIL, Errno::EPROGMISMATCH,
# Errno::EPROGUNAVAIL, Errno::EPROTO, Errno::EPROTONOSUPPORT,
# Errno::EPROTOTYPE, Errno::ERANGE, Errno::EREMOTE, Errno::EROFS,
# Errno::ERPCMISMATCH, Errno::ESHUTDOWN, Errno::ESOCKTNOSUPPORT,
# Errno::ESPIPE, Errno::ESRCH, Errno::ESTALE, Errno::ETIME, Errno::ETIMEDOUT,
# Errno::ETOOMANYREFS, Errno::ETXTBSY, Errno::EUSERS, Errno::EXDEV,
# Errno::NOERROR, Exception, FalseClass, Fiber, FiberError, File, File::Stat,
# Fixnum, Float, FloatDomainError, Gem::BasicSpecification,
# Gem::CommandLineError, Gem::ConflictError, Gem::DependencyError,
# Gem::DependencyRemovalException, Gem::DependencyResolutionError,
# Gem::DocumentError, Gem::EndOfYAMLException, Gem::ErrorReason,
# Gem::Exception, Gem::FilePermissionError, Gem::FormatException,
# Gem::GemNotFoundException, Gem::GemNotInHomeException,
# Gem::ImpossibleDependenciesError, Gem::InstallError,
# Gem::InvalidSpecificationException, Gem::List, Gem::LoadError,
# Gem::OperationNotSupportedError, Gem::Platform, Gem::PlatformMismatch,
# Gem::RemoteError, Gem::RemoteInstallationCancelled,
# Gem::RemoteInstallationSkipped, Gem::RemoteSourceException, Gem::Requirement,
# Gem::Requirement::BadRequirementError, Gem::RubyVersionMismatch,
# Gem::SourceFetchProblem, Gem::SpecificGemNotFoundException,
# Gem::Specification, Gem::StubSpecification, Gem::StubSpecification::StubLine,
# Gem::SystemExitException, Gem::UnsatisfiableDependencyError,
# Gem::VerificationError, Gem::Version, Hash, IO, IO::EAGAINWaitReadable,
# IO::EAGAINWaitWritable, IO::EINPROGRESSWaitReadable,
# IO::EINPROGRESSWaitWritable, IOError, IndexError, Integer, Interrupt,
# KeyError, LoadError, LocalJumpError, MatchData, Math::DomainError, Method,
# Module, Monitor, MonitorMixin::ConditionVariable,
# MonitorMixin::ConditionVariable::Timeout, Mutex, NameError,
# NameError::message, NilClass, NoMemoryError, NoMethodError,
# NotImplementedError, Numeric, Object, ObjectSpace::WeakMap, Proc,
# Process::Status, Process::Tms, Process::Waiter, Random, Range, RangeError,
# Rational, Rational::compatible, Regexp, RegexpError, RubyVM, RubyVM::Env,
# RubyVM::InstructionSequence, RuntimeError, ScriptError, SecurityError,
# SignalException, StandardError, StopIteration, String, StringIO, Struct,
# Symbol, SyntaxError, SystemCallError, SystemExit, SystemStackError, Thread,
# Thread::Backtrace, Thread::Backtrace::Location, Thread::ConditionVariable,
# Thread::Queue, Thread::SizedQueue, ThreadError, ThreadGroup, Time,
# TracePoint, TrueClass, TypeError, UnboundMethod, UncaughtThrowError,
# ZeroDivisionError, fatal]
Note, however, that by artificially restricting yourself to classes, you miss out on some rather important methods which are defined in modules, for example Kernel#puts and Kernel#require.
I removed singleton classes from the list, because it would have become just too cluttered, but unfortunately, this means that you are now missing, for example, using, which is defined in the singleton class of the anonymous top-level object usually called main.
You also miss out on pre-defined values that are not classes or modules, such as Float::INFINITY, true, false, nil, etc.
Note also that this will return all classes loaded, not only those from the core library, but also implementation-specific classes. The whole RubyVM namespace, for example, is a private internal implementation detail of YARV and doesn't exist on other Ruby implementations. Likewise, Fixnum and Bignum are not guaranteed to exist, the Ruby Language Specification only guarantees that there is an Integer class which may or may not have zero or more implementation-specific subclasses.
A better approach to guaranteeing that your code runs on all Ruby implementations would be to restrict yourself to the language, modules and classes defined in the ISO Ruby Language Specification.

Related

How to use pg_search_scope with Mobility for translated attributes?

how can i use pg_search_scope in combination with Mobility for translated attributes? Available languages are :de and :en. Searching for category names return empty results. There mus be a way to search for locale_accessors, in my case it should be name_de || name_en depending on current i18n.locale settings.
Here's my controller method:
def index
#categories = params[:query].present? ? Category.search(params[:query]) : Category.all.includes(%i[string_translations])
end
Here's my model:
class Category < ApplicationRecord
extend Mobility
translates :name, type: :string
include PgSearch::Model
pg_search_scope :search,
against: [:name],
using: {
trigram: { threshold: 0.3, word_similarity: true }
}
end
Here's my mobility config:
Mobility.configure do
# PLUGINS
plugins do
# Backend
#
# Sets the default backend to use in models. This can be overridden in models
# by passing +backend: ...+ to +translates+.
#
# To default to a different backend globally, replace +:key_value+ by another
# backend name.
#
backend :key_value
# ActiveRecord
#
# Defines ActiveRecord as ORM, and enables ActiveRecord-specific plugins.
active_record
# Accessors
#
# Define reader and writer methods for translated attributes. Remove either
# to disable globally, or pass +reader: false+ or +writer: false+ to
# +translates+ in any translated model.
#
reader
writer
# Backend Reader
#
# Defines reader to access the backend for any attribute, of the form
# +<attribute>_backend+.
#
backend_reader
#
# Or pass an interpolation string to define a different pattern:
# backend_reader "%s_translations"
# Query
#
# Defines a scope on the model class which allows querying on
# translated attributes. The default scope is named +i18n+, pass a different
# name as default to change the global default, or to +translates+ in any
# model to change it for that model alone.
#
query
# Cache
#
# Comment out to disable caching reads and writes.
#
cache
# Dirty
#
# Uncomment this line to include and enable globally:
# dirty
#
# Or uncomment this line to include but disable by default, and only enable
# per model by passing +dirty: true+ to +translates+.
# dirty false
# Fallbacks
#
# Uncomment line below to enable fallbacks, using +I18n.fallbacks+.
# fallbacks
#
# Or uncomment this line to enable fallbacks with a global default.
# fallbacks { :pt => :en }
# Presence
#
# Converts blank strings to nil on reads and writes. Comment out to
# disable.
#
presence
# Default
#
# Set a default translation per attributes. When enabled, passing +default:
# 'foo'+ sets a default translation string to show in case no translation is
# present. Can also be passed a proc.
#
# default 'foo'
# Fallthrough Accessors
#
# Uses method_missing to define locale-specific accessor methods like
# +title_en+, +title_en=+, +title_fr+, +title_fr=+ for each translated
# attribute. If you know what set of locales you want to support, it's
# generally better to use Locale Accessors (or both together) since
# +method_missing+ is very slow. (You can use both fallthrough and locale
# accessor plugins together without conflict.)
#
# fallthrough_accessors
# Locale Accessors
#
# Uses +def+ to define accessor methods for a set of locales. By default uses
# +I18n.available_locales+, but you can pass the set of locales with
# +translates+ and/or set a global default here.
#
locale_accessors
#
# Or define specific defaults by uncommenting line below
# locale_accessors [:en, :ja]
# Attribute Methods
#
# Adds translated attributes to +attributes+ hash, and defines methods
# +translated_attributes+ and +untranslated_attributes+ which return hashes
# with translated and untranslated attributes, respectively. Be aware that
# this plugin can create conflicts with other gems.
#
# attribute_methods
end
end
Is it possible and how it works?
Thank you very much for help me out in this case!
You can use dynamic scope with lambda and return the config hash with the query:
pg_search_scope :search, lambda { |query|
{
against: ["name_#{I18n.locale}".to_sym],
trigram: { threshold: 0.3, word_similarity: true },
ignoring: :accents,
query: query
}
}

Object.respond_to? fails when retrieving method name symbol from array

I'm implementing page objects, and writing tests to verify them. I want to simplify the tests by storing the element names in an array of symbols and looping through it, but it's failing.
def setup
#browser = Watir::Browser.new :phantomjs
#export_page = ExportPage.new #browser
#assets = %i{:section :brand}
end
--
#PASSES
def test_static
$stdout.puts :section.object_id
raise PageElementSelectorNotFoundException, :section unless #export_page.respond_to? :section
end
> # 2123548
This passes because the target class does implement this method, however:
#FAILS
def test_iterator
#assets.each do |selector|
$stdout.puts selector.class
$stdout.puts selector.object_id
$stdout.puts :section.object_id
raise PageElementSelectorNotFoundException, selector unless #export_page.respond_to? selector
end
end
> # Testing started at 11:19 ...
> # Symbol
> # 2387188
> # 2123548
PageElementSelectorNotFoundException: :section missing from page
~/src/stories/test/pages/export_page_test.rb:20:in `block in test_iterator'
As you can see, I've checked the object ids of the symbols and they do seem to be different. Could this be why it's failing? Is there a solution to this?
When using short notation to declare an array of atoms, one should not put colons there:
- %i{:section :brand} # incorrect
+ %i{section brand} # correct
What you have actually defined by #assets = %i{:section :brand}, is the following array:
[:':section', :':brand']
Don't use the %i{} notation, because it automatically makes a symbol of the literal(s) that you specify.
This translates to:
#assets = [:":section", :":brand"]
which is technically an array of symbols, just not the symbols that you intended. This is why the object IDs don't match in your tests.
The %i{} syntax was added in Ruby 2.0. When used in code that may support for older Ruby versions, use a traditional array of symbols:
#assets = [:section, :brand]

Ruby leaked objects are referenced by RubyVm::Env

I am tracing a memory leak problem in our application (ruby 2.1). I am using both techniques: ObjectSpace.dump_all for dumping all objects to JSON stream then do an offline analysis. The second technique I used is live analysis with ObjectSpace.reachable_objects_from. In both ways, I found that my leaked objects are referenced by an object RubyVM::Env. Anyone could explain to me what is RubyVM::Env. How to remove those references?
RubyVM::Env is an internal ruby class that holds variable references. Here is my test:
require 'objspace'
a = Object.new
a_id = a.object_id # we use #object_id to avoid creating more reference to `a`
ObjectSpace.each_object.select{ |o| ObjectSpace.reachable_objects_from(o).map(&:object_id).include?(a_id) }.count
# => 1
env = ObjectSpace.each_object.select{ |o| ObjectSpace.reachable_objects_from(o).map(&:object_id).include?(a_id) }.first
# => #<RubyVM::Env:0x007ff39ac09a78>
ObjectSpace.reachable_objects_from(env).count
# => 5
a = nil # remove reference
ObjectSpace.reachable_objects_from(env).count
# => 4

running puppet psych errors

I'm trying to run a puppet apply and thrown with bunch of errors.
hiera -c /etc/puppet/hiera.yaml classes
generates the following :
/usr/share/ruby/vendor_ruby/2.0/psych.rb:205:in `parse': (<unknown>): found character that cannot start any token while scanning for the next token at line 238 column 3 (Psych::SyntaxError)
from /usr/share/ruby/vendor_ruby/2.0/psych.rb:205:in `parse_stream'
from /usr/share/ruby/vendor_ruby/2.0/psych.rb:153:in `parse'
from /usr/share/ruby/vendor_ruby/2.0/psych.rb:129:in `load'
from /usr/local/share/ruby/gems/2.0/gems/hiera-1.3.4/lib/hiera/backend/yaml_backend.rb:18:in `block (2 levels) in lookup'
from /usr/local/share/ruby/gems/2.0/gems/hiera-1.3.4/lib/hiera/filecache.rb:53:in `read_file'
from /usr/local/share/ruby/gems/2.0/gems/hiera-1.3.4/lib/hiera/backend/yaml_backend.rb:17:in `block in lookup'
from /usr/local/share/ruby/gems/2.0/gems/hiera-1.3.4/lib/hiera/backend.rb:104:in `block in datasourcefiles'
from /usr/local/share/ruby/gems/2.0/gems/hiera-1.3.4/lib/hiera/backend.rb:76:in `block in datasources'
from /usr/local/share/ruby/gems/2.0/gems/hiera-1.3.4/lib/hiera/backend.rb:74:in `map'
from /usr/local/share/ruby/gems/2.0/gems/hiera-1.3.4/lib/hiera/backend.rb:74:in `datasources'
from /usr/local/share/ruby/gems/2.0/gems/hiera-1.3.4/lib/hiera/backend.rb:99:in `datasourcefiles'
from /usr/local/share/ruby/gems/2.0/gems/hiera-1.3.4/lib/hiera/backend/yaml_backend.rb:16:in `lookup'
from /usr/local/share/ruby/gems/2.0/gems/hiera-1.3.4/lib/hiera/backend.rb:206:in `block in lookup'
from /usr/local/share/ruby/gems/2.0/gems/hiera-1.3.4/lib/hiera/backend.rb:203:in `each'
from /usr/local/share/ruby/gems/2.0/gems/hiera-1.3.4/lib/hiera/backend.rb:203:in `lookup'
from /usr/local/share/ruby/gems/2.0/gems/hiera-1.3.4/lib/hiera.rb:60:in `lookup'
from /usr/local/share/ruby/gems/2.0/gems/hiera-1.3.4/bin/hiera:225:in `<top (required)>'
from /usr/local/bin/hiera:23:in `load'
from /usr/local/bin/hiera:23:in `<main>'
contents of the file:
require 'psych.so'
require 'psych/nodes'
require 'psych/streaming'
require 'psych/visitors'
require 'psych/handler'
require 'psych/tree_builder'
require 'psych/parser'
require 'psych/omap'
require 'psych/set'
require 'psych/coder'
require 'psych/core_ext'
require 'psych/deprecated'
require 'psych/stream'
require 'psych/json/tree_builder'
require 'psych/json/stream'
require 'psych/handlers/document_stream'
###
# = Overview
#
# Psych is a YAML parser and emitter.
# Psych leverages libyaml [Home page: http://pyyaml.org/wiki/LibYAML]
# or [Git repo: https://github.com/zerotao/libyaml] for its YAML parsing
# and emitting capabilities. In addition to wrapping libyaml, Psych also
# knows how to serialize and de-serialize most Ruby objects to and from
# the YAML format.
#
# = I NEED TO PARSE OR EMIT YAML RIGHT NOW!
#
# # Parse some YAML
# Psych.load("--- foo") # => "foo"
#
# # Emit some YAML
# Psych.dump("foo") # => "--- foo\n...\n"
# { :a => 'b'}.to_yaml # => "---\n:a: b\n"
#
# Got more time on your hands? Keep on reading!
#
# == YAML Parsing
#
# Psych provides a range of interfaces for parsing a YAML document ranging from
# low level to high level, depending on your parsing needs. At the lowest
# level, is an event based parser. Mid level is access to the raw YAML AST,
# and at the highest level is the ability to unmarshal YAML to ruby objects.
#
# === Low level parsing
#
# The lowest level parser should be used when the YAML input is already known,
# and the developer does not want to pay the price of building an AST or
# automatic detection and conversion to ruby objects. See Psych::Parser for
# more information on using the event based parser.
#
# === Mid level parsing
#
# Psych provides access to an AST produced from parsing a YAML document. This
# tree is built using the Psych::Parser and Psych::TreeBuilder. The AST can
# be examined and manipulated freely. Please see Psych::parse_stream,
# Psych::Nodes, and Psych::Nodes::Node for more information on dealing with
# YAML syntax trees.
#
# === High level parsing
#
# The high level YAML parser provided by Psych simply takes YAML as input and
# returns a Ruby data structure. For information on using the high level parser
# see Psych.load
#
# == YAML Emitting
#
# Psych provides a range of interfaces ranging from low to high level for
# producing YAML documents. Very similar to the YAML parsing interfaces, Psych
# provides at the lowest level, an event based system, mid-level is building
# a YAML AST, and the highest level is converting a Ruby object straight to
# a YAML document.
#
# === Low level emitting
#
# The lowest level emitter is an event based system. Events are sent to a
# Psych::Emitter object. That object knows how to convert the events to a YAML
# document. This interface should be used when document format is known in
# advance or speed is a concern. See Psych::Emitter for more information.
#
# === Mid level emitting
#
# At the mid level is building an AST. This AST is exactly the same as the AST
# used when parsing a YAML document. Users can build an AST by hand and the
# AST knows how to emit itself as a YAML document. See Psych::Nodes,
# Psych::Nodes::Node, and Psych::TreeBuilder for more information on building
# a YAML AST.
#
# === High level emitting
#
# The high level emitter has the easiest interface. Psych simply takes a Ruby
# data structure and converts it to a YAML document. See Psych.dump for more
# information on dumping a Ruby data structure.
module Psych
# The version is Psych you're using
VERSION = '2.0.0'
# The version of libyaml Psych is using
LIBYAML_VERSION = Psych.libyaml_version.join '.'
class Exception < RuntimeError
end
class BadAlias < Exception
end
###
# Load +yaml+ in to a Ruby data structure. If multiple documents are
# provided, the object contained in the first document will be returned.
# +filename+ will be used in the exception message if any exception is raised
# while parsing.
#
# Raises a Psych::SyntaxError when a YAML syntax error is detected.
#
# Example:
#
# Psych.load("--- a") # => 'a'
# Psych.load("---\n - a\n - b") # => ['a', 'b']
#
# begin
# Psych.load("--- `", "file.txt")
# rescue Psych::SyntaxError => ex
# ex.file # => 'file.txt'
# ex.message # => "(file.txt): found character that cannot start any token"
# end
def self.load yaml, filename = nil
result = parse(yaml, filename)
result ? result.to_ruby : result
end
###
# Parse a YAML string in +yaml+. Returns the first object of a YAML AST.
# +filename+ is used in the exception message if a Psych::SyntaxError is
# raised.
#
# Raises a Psych::SyntaxError when a YAML syntax error is detected.
#
# Example:
#
# Psych.parse("---\n - a\n - b") # => #<Psych::Nodes::Sequence:0x00>
#
# begin
# Psych.parse("--- `", "file.txt")
# rescue Psych::SyntaxError => ex
# ex.file # => 'file.txt'
# ex.message # => "(file.txt): found character that cannot start any token"
# end
#
# See Psych::Nodes for more information about YAML AST.
def self.parse yaml, filename = nil
parse_stream(yaml, filename) do |node|
return node
end
false
end
###
# Parse a file at +filename+. Returns the YAML AST.
#
# Raises a Psych::SyntaxError when a YAML syntax error is detected.
def self.parse_file filename
File.open filename, 'r:bom|utf-8' do |f|
parse f, filename
end
end
###
# Returns a default parser
def self.parser
Psych::Parser.new(TreeBuilder.new)
end
###
# Parse a YAML string in +yaml+. Returns the full AST for the YAML document.
# This method can handle multiple YAML documents contained in +yaml+.
# +filename+ is used in the exception message if a Psych::SyntaxError is
# raised.
#
# If a block is given, a Psych::Nodes::Document node will be yielded to the
# block as it's being parsed.
#
# Raises a Psych::SyntaxError when a YAML syntax error is detected.
#
# Example:
#
# Psych.parse_stream("---\n - a\n - b") # => #<Psych::Nodes::Stream:0x00>
#
# Psych.parse_stream("--- a\n--- b") do |node|
# node # => #<Psych::Nodes::Document:0x00>
# end
#
# begin
# Psych.parse_stream("--- `", "file.txt")
# rescue Psych::SyntaxError => ex
# ex.file # => 'file.txt'
# ex.message # => "(file.txt): found character that cannot start any token"
# end
#
# See Psych::Nodes for more information about YAML AST.
def self.parse_stream yaml, filename = nil, &block
if block_given?
parser = Psych::Parser.new(Handlers::DocumentStream.new(&block))
parser.parse yaml, filename
else
parser = self.parser
parser.parse yaml, filename
parser.handler.root
end
end
###
# call-seq:
# Psych.dump(o) -> string of yaml
# Psych.dump(o, options) -> string of yaml
# Psych.dump(o, io) -> io object passed in
# Psych.dump(o, io, options) -> io object passed in
#
# Dump Ruby object +o+ to a YAML string. Optional +options+ may be passed in
# to control the output format. If an IO object is passed in, the YAML will
# be dumped to that IO object.
#
# Example:
#
# # Dump an array, get back a YAML string
# Psych.dump(['a', 'b']) # => "---\n- a\n- b\n"
#
# # Dump an array to an IO object
# Psych.dump(['a', 'b'], StringIO.new) # => #<StringIO:0x000001009d0890>
#
# # Dump an array with indentation set
# Psych.dump(['a', ['b']], :indentation => 3) # => "---\n- a\n- - b\n"
#
# # Dump an array to an IO with indentation set
# Psych.dump(['a', ['b']], StringIO.new, :indentation => 3)
#def self.dump o, io = nil, options = {}
def self.dump o, io = nil, options = {}
if Hash === io
options = io
io = nil
end
visitor = Psych::Visitors::YAMLTree.new options
visitor << o
visitor.tree.yaml io, options
end
###
# Dump a list of objects as separate documents to a document stream.
#
# Example:
#
# Psych.dump_stream("foo\n ", {}) # => "--- ! \"foo\\n \"\n--- {}\n"
def self.dump_stream *objects
visitor = Psych::Visitors::YAMLTree.new {}
objects.each do |o|
visitor << o
end
visitor.tree.yaml
end
###
# Dump Ruby object +o+ to a JSON string.
def self.to_json o
visitor = Psych::Visitors::JSONTree.new
visitor << o
visitor.tree.yaml
end
###
# Load multiple documents given in +yaml+. Returns the parsed documents
# as a list. If a block is given, each document will be converted to ruby
# and passed to the block during parsing
#
# Example:
#
# Psych.load_stream("--- foo\n...\n--- bar\n...") # => ['foo', 'bar']
#
# list = []
# Psych.load_stream("--- foo\n...\n--- bar\n...") do |ruby|
# list << ruby
# end
# list # => ['foo', 'bar']
#
def self.load_stream yaml, filename = nil
if block_given?
parse_stream(yaml, filename) do |node|
yield node.to_ruby
end
else
parse_stream(yaml, filename).children.map { |child| child.to_ruby }
end
end
###
# Load the document contained in +filename+. Returns the yaml contained in
# +filename+ as a ruby object
def self.load_file filename
File.open(filename, 'r:bom|utf-8') { |f| self.load f, filename }
end
# :stopdoc:
#domain_types = {}
def self.add_domain_type domain, type_tag, &block
key = ['tag', domain, type_tag].join ':'
#domain_types[key] = [key, block]
#domain_types["tag:#{type_tag}"] = [key, block]
end
def self.add_builtin_type type_tag, &block
domain = 'yaml.org,2002'
key = ['tag', domain, type_tag].join ':'
#domain_types[key] = [key, block]
end
def self.remove_type type_tag
#domain_types.delete type_tag
end
#load_tags = {}
#dump_tags = {}
def self.add_tag tag, klass
#load_tags[tag] = klass
#dump_tags[klass] = tag
end
class << self
attr_accessor :load_tags
attr_accessor :dump_tags
attr_accessor :domain_types
end
# :startdoc:
end
I have seen some references quoting
require 'yaml'
YAML::ENGINE.yamler = 'syck'
cannot find a config/boot.rb to try this.
I ripped out system wide ruby and the gems. Trying rvm and i still get similar errors :
I'm running hiera in debug mode :
[lame#TTBOX ~]$ hiera -d -c eye
Cannot find config file: eye
[lame#ipTTBOX ~]$ hiera -d eye
DEBUG: 2014-11-25 05:47:37 +0700: Hiera YAML backend starting
DEBUG: 2014-11-25 05:47:37 +0700: Looking up eye in YAML backend
DEBUG: 2014-11-25 05:47:37 +0700: Looking for data source default
/home/lame/.rvm/gems/ruby-2.1.5/gems/psych-2.0.6/lib/psych.rb:370:in `parse': (<unknown>): found character that cannot start any token while scanning for the next token at line 238 column 3 (Psych::SyntaxError)
from /home/lame/.rvm/gems/ruby-2.1.5/gems/psych-2.0.6/lib/psych.rb:370:in `parse_stream'
from /home/lame/.rvm/gems/ruby-2.1.5/gems/psych-2.0.6/lib/psych.rb:318:in `parse'
from /home/lame/.rvm/gems/ruby-2.1.5/gems/psych-2.0.6/lib/psych.rb:245:in `load'
from /home/lame/.rvm/gems/ruby-2.1.5/gems/hiera-1.3.4/lib/hiera/backend/yaml_backend.rb:18:in `block (2 levels) in lookup'
from /home/lame/.rvm/gems/ruby-2.1.5/gems/hiera-1.3.4/lib/hiera/filecache.rb:53:in `read_file'
from /home/lame/.rvm/gems/ruby-2.1.5/gems/hiera-1.3.4/lib/hiera/backend/yaml_backend.rb:17:in `block in lookup'
from /home/lame/.rvm/gems/ruby-2.1.5/gems/hiera-1.3.4/lib/hiera/backend.rb:104:in `block in datasourcefiles'
from /home/lame/.rvm/gems/ruby-2.1.5/gems/hiera-1.3.4/lib/hiera/backend.rb:76:in `block in datasources'
from /home/lame/.rvm/gems/ruby-2.1.5/gems/hiera-1.3.4/lib/hiera/backend.rb:74:in `map'
from /home/lame/.rvm/gems/ruby-2.1.5/gems/hiera-1.3.4/lib/hiera/backend.rb:74:in `datasources'
from /home/lame/.rvm/gems/ruby-2.1.5/gems/hiera-1.3.4/lib/hiera/backend.rb:99:in `datasourcefiles'
from /home/lame/.rvm/gems/ruby-2.1.5/gems/hiera-1.3.4/lib/hiera/backend/yaml_backend.rb:16:in `lookup'
from /home/lame/.rvm/gems/ruby-2.1.5/gems/hiera-1.3.4/lib/hiera/backend.rb:206:in `block in lookup'
from /home/lame/.rvm/gems/ruby-2.1.5/gems/hiera-1.3.4/lib/hiera/backend.rb:203:in `each'
from /home/lame/.rvm/gems/ruby-2.1.5/gems/hiera-1.3.4/lib/hiera/backend.rb:203:in `lookup'
from /home/lame/.rvm/gems/ruby-2.1.5/gems/hiera-1.3.4/lib/hiera.rb:60:in `lookup'
from /home/lame/.rvm/gems/ruby-2.1.5/gems/hiera-1.3.4/bin/hiera:225:in `<top (required)>'
from /home/lame/.rvm/gems/ruby-2.1.5/bin/hiera:23:in `load'
from /home/lame/.rvm/gems/ruby-2.1.5/bin/hiera:23:in `<main>'
from /home/lame/.rvm/gems/ruby-2.1.5/bin/ruby_executable_hooks:15:in `eval'
from /home/lame/.rvm/gems/ruby-2.1.5/bin/ruby_executable_hooks:15:in `<main>'
Are you suggesting me to have a look at the hiera file itself ?
Your error message says to look at line 238, column 3 of your yaml file. Check to see if you have any illegal characters there.

Can I manipulate yaml files and write them out again

I have a map of values, the key is a filename and the value is an array strings.
I have the corresponding files
how would I load the file and create a fixed yaml value which contains the value of the array whether or not the value already exists
e.g.
YAML (file.yaml)
trg::azimuth:
-extra
-intra
-lateral
or
trg::azimuth:
[extra,intra,lateral]
from
RUBY
{"file.yaml" => ["extra","intra","lateral"]}
The YAML documentation doesn't cover its methods very well, but does say
The underlying implementation is the libyaml wrapper Psych.
The Psych documentation, which underlies YAML, covers reading, parsing, and emitting YAML.
Here's the basic process:
require 'yaml'
foo = {"file.yaml" => ["extra","intra","lateral"]}
bar = foo.to_yaml
# => "---\nfile.yaml:\n- extra\n- intra\n- lateral\n"
And here's what the generated, serialized bar variable looks like if written:
puts bar
# >> ---
# >> file.yaml:
# >> - extra
# >> - intra
# >> - lateral
That's the format a YAML parser needs:
baz = YAML.load(bar)
baz
# => {"file.yaml"=>["extra", "intra", "lateral"]}
At this point the hash has gone round-trip, from a Ruby hash, to a YAML-serialized string, back to a Ruby hash.
Writing YAML to a file is easy using Ruby's File.write method:
File.write(foo.keys.first, foo.values.first.to_yaml)
or
foo.each do |k, v|
File.write(k, v.to_yaml)
end
Which results in a file named "file.yaml", which contains:
---
- extra
- intra
- lateral
To read and parse a file, use YAML's load_file method.
foo = YAML.load_file('file.yaml')
# => ["extra", "intra", "lateral"]
"How do I parse a YAML file?" might be of use, as well as the other "Related" links on the right side of this page.

Resources