Module: JSI::Util Private
Overview
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
JSI::Util classes, modules, constants, and methods are internal, and will be added and removed without warning.
Defined Under Namespace
Modules: FingerprintHash, Memoize, Virtual Classes: AttrStruct, MemoMap
Instance Method Summary collapse
- #deep_stringify_symbol_keys(object) ⇒ Object private
-
#ensure_module_set(modules) ⇒ Set
private
ensures the given param becomes a frozen Set of Modules.
-
#ok_ruby_method_name?(name) ⇒ Boolean
private
is the given name ok to use as a ruby method name?.
-
#stringify_symbol_keys(hashlike) ⇒ same class as the param `hash`, or Hash if the former cannot be done
private
a hash copied from the given hashlike, in which any symbol keys are converted to strings.
-
#ycomb ⇒ Object
private
this is the Y-combinator, which allows anonymous recursive functions.
Instance Method Details
#deep_stringify_symbol_keys(object) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/jsi/util.rb', line 37 def deep_stringify_symbol_keys(object) if object.respond_to?(:to_hash) JSI::Typelike.modified_copy(object) do |hash| out = {} (hash.respond_to?(:each) ? hash : hash.to_hash).each do |k, v| out[k.is_a?(Symbol) ? k.to_s : deep_stringify_symbol_keys(k)] = deep_stringify_symbol_keys(v) end out end elsif object.respond_to?(:to_ary) JSI::Typelike.modified_copy(object) do |ary| (ary.respond_to?(:each) ? ary : ary.to_ary).map do |e| deep_stringify_symbol_keys(e) end end else object end end |
#ensure_module_set(modules) ⇒ Set
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
ensures the given param becomes a frozen Set of Modules. returns the param if it is already that, otherwise initializes and freezes such a Set.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/jsi/util.rb', line 64 def ensure_module_set(modules) if modules.is_a?(Set) && modules.frozen? set = modules else set = Set.new(modules).freeze end not_modules = set.reject { |s| s.is_a?(Module) } if !not_modules.empty? raise(TypeError, [ "ensure_module_set given non-Module objects:", *not_modules.map { |ns| ns.pretty_inspect.chomp }, ].join("\n")) end set end |
#ok_ruby_method_name?(name) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
is the given name ok to use as a ruby method name?
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/jsi/util.rb', line 82 def ok_ruby_method_name?(name) # must be a string return false unless name.respond_to?(:to_str) # must not begin with a digit return false if name =~ /\A[0-9]/ # must not contain characters special to ruby syntax return false if name =~ /[\\\s\#;\.,\(\)\[\]\{\}'"`%\+\-\/\*\^\|&=<>\?:!@\$~]/ return true end |
#stringify_symbol_keys(hashlike) ⇒ same class as the param `hash`, or Hash if the former cannot be done
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
a hash copied from the given hashlike, in which any symbol keys are converted to strings. behavior on collisions is undefined (but in the future could take a block like ActiveSupport::HashWithIndifferentAccess#update)
at the moment it is undefined whether the returned hash is the same
instance as the hash
param. if hash
is already a hash which contains
no symbol keys, this method MAY return that same instance. use #dup on
the return if you need to ensure it is not the same instance as the
argument instance.
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/jsi/util.rb', line 24 def stringify_symbol_keys(hashlike) unless hashlike.respond_to?(:to_hash) raise(ArgumentError, "expected argument to be a hash; got #{hashlike.class.inspect}: #{hashlike.pretty_inspect.chomp}") end JSI::Typelike.modified_copy(hashlike) do |hash| out = {} hash.each do |k, v| out[k.is_a?(Symbol) ? k.to_s : k] = v end out end end |
#ycomb ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
this is the Y-combinator, which allows anonymous recursive functions. for a simple example, to define a recursive function to return the length of an array:
length = ycomb do |len|
proc { |list| list == [] ? 0 : 1 + len.call(list[1..-1]) }
end
length.call([0])
# => 1
see https://en.wikipedia.org/wiki/Fixed-point_combinator#Y_combinator and chapter 9 of the little schemer, available as the sample chapter at https://felleisen.org/matthias/BTLS-index.html
106 107 108 |
# File 'lib/jsi/util.rb', line 106 def ycomb proc { |f| f.call(f) }.call(proc { |f| yield proc { |*x| f.call(f).call(*x) } }) end |