summaryrefslogtreecommitdiff
path: root/prelude.rb
blob: 55b42f06c4f54d78409a8fb7cfc674b4a80a8289 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
66
67
68
class Binding
  # :nodoc:
  def irb
    begin
      require 'irb'
    rescue LoadError, Gem::LoadError
      force_activate 'irb'
      retry
    end
    irb
  end

  # suppress redefinition warning
  alias irb irb # :nodoc:

  private def force_activate(gem)
    Bundler.reset!

    builder = Bundler::Dsl.new
    if Bundler.definition.gemfiles.empty? # bundler/inline
      Bundler.definition.locked_gems.specs.each{|spec| builder.gem spec.name, spec.version.to_s }
    else
      Bundler.definition.gemfiles.each{|gemfile| builder.eval_gemfile(gemfile) }
    end
    builder.gem gem

    definition = builder.to_definition(nil, true)
    definition.validate_runtime!

    begin
      orig_ui = Bundler.ui
      orig_no_lock = Bundler::Definition.no_lock

      ui = Bundler::UI::Shell.new
      ui.level = "silent"
      Bundler.ui = ui
      Bundler::Definition.no_lock = true

      Bundler::Runtime.new(nil, definition).setup
    rescue Bundler::GemNotFound
      warn "Failed to activate #{gem}, please install it with 'gem install #{gem}'"
    ensure
      Bundler.ui = orig_ui
      Bundler::Definition.no_lock = orig_no_lock
    end
  end
end

module Kernel
  def pp(*objs)
    require 'pp'
    pp(*objs)
  end

  # suppress redefinition warning
  alias pp pp # :nodoc:

  private :pp
end

autoload :Set, 'set'

module Enumerable
  # Makes a set from the enumerable object with given arguments.
  def to_set(klass = Set, *args, &block)
    klass.new(self, *args, &block)
  end unless instance_methods.include?(:to_set) # RJIT could already load this from builtin prelude
end