lib/jsi/util.rb in jsi-0.3.0 vs lib/jsi/util.rb in jsi-0.4.0

- old
+ new

@@ -1,8 +1,10 @@ # frozen_string_literal: true module JSI + # JSI::Util classes, modules, constants, and methods are INTERNAL and will be added and removed without warning. + # do not rely on them. module Util # a proc which does nothing NOOP = -> (*_) { } # returns a version of the given hash, in which any symbol keys are @@ -14,11 +16,11 @@ # 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. # - # @param hash [#to_hash] the hash from which to convert symbol keys to strings + # @param hashlike [#to_hash] the hash from which to convert symbol keys to strings # @return [same class as the param `hash`, or Hash if the former cannot be done] a # hash(-like) instance containing no symbol keys 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}") @@ -63,45 +65,44 @@ # and chapter 9 of the little schemer, available as the sample chapter at http://www.ccs.neu.edu/home/matthias/BTLS/ def ycomb proc { |f| f.call(f) }.call(proc { |f| yield proc { |*x| f.call(f).call(*x) } }) end module_function :ycomb - end - public - extend Util - module FingerprintHash - # overrides BasicObject#== - def ==(other) - object_id == other.object_id || (other.respond_to?(:jsi_fingerprint) && other.jsi_fingerprint == self.jsi_fingerprint) - end + module FingerprintHash + # overrides BasicObject#== + def ==(other) + object_id == other.object_id || (other.respond_to?(:jsi_fingerprint) && other.jsi_fingerprint == self.jsi_fingerprint) + end - alias_method :eql?, :== + alias_method :eql?, :== - # overrides Kernel#hash - def hash - jsi_fingerprint.hash + # overrides Kernel#hash + def hash + jsi_fingerprint.hash + end end - end - module Memoize - def jsi_memoize(key, *args_) - @jsi_memos ||= {} - @jsi_memos[key] ||= Hash.new do |h, args| - h[args] = yield(*args) + module Memoize + def jsi_memoize(key, *args_) + @jsi_memos ||= {} + @jsi_memos[key] ||= Hash.new do |h, args| + h[args] = yield(*args) + end + @jsi_memos[key][args_] end - @jsi_memos[key][args_] - end - def jsi_clear_memo(key, *args) - @jsi_memos ||= {} - if @jsi_memos[key] - if args.empty? - @jsi_memos[key].clear - else - @jsi_memos[key].delete(args) + def jsi_clear_memo(key, *args) + @jsi_memos ||= {} + if @jsi_memos[key] + if args.empty? + @jsi_memos[key].clear + else + @jsi_memos[key].delete(args) + end end end end end - extend Memoize + public + extend Util end