Module: RubyNext::Utils

Included in:
Language::Runtime
Defined in:
lib/ruby-next/utils.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.refine_modules?Boolean

Returns true if modules refinement is supported in current version

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ruby-next/utils.rb', line 38

def refine_modules?
  @refine_modules ||=
    begin
      # Make sure that including modules within refinements works
      # See https://github.com/oracle/truffleruby/issues/2026
      eval <<~RUBY, TOPLEVEL_BINDING, __FILE__, __LINE__ + 1
        module RubyNext::Utils::A; end
        class RubyNext::Utils::B
          include RubyNext::Utils::A
        end

        using(Module.new do
          refine RubyNext::Utils::A do
            include(Module.new do
              def i_am_refinement
                "yes, you are!"
              end
            end)
          end
        end)

        RubyNext::Utils::B.new.i_am_refinement
      RUBY
      true
    rescue TypeError, NoMethodError
      false
    end
end

.source_with_lines(source, path) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/ruby-next/utils.rb', line 29

def source_with_lines(source, path)
  source.lines.map.with_index do |line, i|
    "#{(i + 1).to_s.rjust(4)}:  #{line}"
  end.tap do |lines|
    lines.unshift "   0:  # source: #{path}"
  end
end

Instance Method Details

#resolve_feature_path(path) ⇒ Object



8
9
10
11
# File 'lib/ruby-next/utils.rb', line 8

def resolve_feature_path(feature)
  $LOAD_PATH.resolve_feature_path(feature)&.last
rescue LoadError
end