Module: JSI::Util::Private Private
- Extended by:
- Private
- Defined in:
- lib/jsi/util/private.rb,
lib/jsi/util/private/attr_struct.rb
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::Private 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
Constant Summary collapse
- EMPTY_ARY =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
[].freeze
- EMPTY_SET =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Set[].freeze
- LAST_ARGUMENT_AS_KEYWORD_PARAMETERS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
is a hash as the last argument passed to keyword params? (false in ruby 3; true before - generates a warning in 2.7 but no way to make 2.7 behave like 3 so the warning is useless)
TODO remove eventually (keyword argument compatibility)
begin if Object.const_defined?(:Warning) warn = ::Warning.instance_method(:warn) ::Warning.send(:remove_method, :warn) ::Warning.send(:define_method, :warn) { |*, **| } end -> (k: ) { k }[{k: nil}] true rescue ArgumentError false ensure if Object.const_defined?(:Warning) ::Warning.send(:remove_method, :warn) ::Warning.send(:define_method, :warn, warn) end end
Instance Method Summary collapse
-
#ok_ruby_method_name?(name) ⇒ Boolean
private
is the given name ok to use as a ruby method name?.
- #require_jmespath ⇒ Object private
-
#ycomb ⇒ Object
private
this is the Y-combinator, which allows anonymous recursive functions.
Instance Method Details
#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?
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/jsi/util/private.rb', line 37 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 |
#require_jmespath ⇒ 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.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/jsi/util/private.rb', line 65 def require_jmespath return if instance_variable_defined?(:@jmespath_required) begin require 'jmespath' rescue ::LoadError => e # :nocov: msg = [ "please install and/or add to your Gemfile the `jmespath` gem to use this. jmespath is not a dependency of JSI.", "original error message:", e., ].join("\n") raise(e.class, msg, e.backtrace) # :nocov: end hashlike = JSI::SchemaSet[].new_jsi({'test' => 0}) unless JMESPath.search('test', hashlike) == 0 # :nocov: raise(::LoadError, [ "the loaded version of jmespath cannot be used with JSI.", "jmespath is compatible with JSI objects as of version 1.5.0", ].join("\n")) # :nocov: end @jmespath_required = true nil 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
61 62 63 |
# File 'lib/jsi/util/private.rb', line 61 def ycomb proc { |f| f.call(f) }.call(proc { |f| yield proc { |*x| f.call(f).call(*x) } }) end |