Module: Memorb::RubyCompatibility
- Defined in:
- lib/memorb/ruby_compatibility.rb
Overview
When Memorb code needs to be changed to accomodate older Ruby versions, that code should live here. This has a few advantages:
- it will be clear that there was a need to change the code for Ruby
compatibility reasons
- when the Ruby versions that required the altered code will no longer
be supported, locating the places to refactor is easy
- the specific approach needed to accommodate older version of Ruby
and any explanatory comments need only be defined in one place
Class Method Summary collapse
-
.module_constant(receiver, key) ⇒ Object
JRuby * JRuby doesn’t work well with module singleton constants, so an instance variable is used instead.
- .module_constant_set(receiver, key, value) ⇒ Object
-
.name_error_matches(error, expected_name, expected_receiver) ⇒ Object
JRuby * JRuby does not yet support ‘receiver` on `NameError`.
Class Method Details
.module_constant(receiver, key) ⇒ Object
JRuby * JRuby doesn’t work well with module singleton constants, so an instance variable is used instead.
28 29 30 |
# File 'lib/memorb/ruby_compatibility.rb', line 28 def module_constant(receiver, key) receiver.instance_variable_get(ivar_name(key)) end |
.module_constant_set(receiver, key, value) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/memorb/ruby_compatibility.rb', line 31 def module_constant_set(receiver, key, value) n = ivar_name(key) if receiver.instance_variable_defined?(n) raise "Memorb internal error! Reassignment of constant at #{ n }" end receiver.instance_variable_set(n, value) end |
.name_error_matches(error, expected_name, expected_receiver) ⇒ Object
JRuby * JRuby does not yet support ‘receiver` on `NameError`. github.com/jruby/jruby/issues/5576
42 43 44 45 |
# File 'lib/memorb/ruby_compatibility.rb', line 42 def name_error_matches(error, expected_name, expected_receiver) return false unless error.name.to_s == expected_name.to_s ::RUBY_ENGINE == 'jruby' || error.receiver.equal?(expected_receiver) end |