loading
main.rb
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
class Foo
  def instance_method
    puts "From instance method"
  end

  def self.class_method
    puts "From class method"
  end

  class << self
    def class_injected_method
      puts "Class injected method"
    end
  end
end

puts "Creating a new instance f"
f = Foo.new

puts 'checking class and instance methods'
Foo.class_method
f.instance_method

puts "Creating a new instance g"
g = Foo.new

puts 'Adding a method to singleton class of f'
class << f
  def injected_method
    puts "From injected method"
  end
end

puts "f.respond_to? " + f.respond_to?(:injected_method).to_s
puts "g.respond_to? " + g.respond_to?(:injected_method).to_s

puts 'calling the injected method'
f.injected_method
ruby 2.5.5p157 (2019-03-15 revision 67260) [x86_64-linux]