Ubuntu Pastebin

Paste from a at Tue, 15 Dec 2015 11:07:20 +0000

Download as text
 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
class Object
  alias print puts

  def setter_block(name)
    ->(*args) { instance_variable_get(:@__attributes__)[name] = *args }
  end

  def getter_block(name)
    ->(*args) { instance_variable_get(:@__attributes__)[name] }
  end

  def method_missing(id, *args, &block)
    method_name = id.to_s
    if method_name.end_with?('=') &&
        instance_variable_defined?(:@__attributes__)
      attribute_name = method_name.chop
      define_singleton_method(method_name, setter_block(attribute_name))
      define_singleton_method(attribute_name, getter_block(attribute_name))
      send(method_name, *args, &block)
    elsif Object.const_defined?(id)
      obj = Object.const_get(id).allocate
      obj.instance_variable_set(:@__attributes__, {})
      obj.__init__(obj, *args, &block)
      obj
    else
      super
    end
  rescue NameError
    super
  end
end

class Person
  def __init__(se1f, name, age)
    se1f.name = name
    se1f.age = age end end

person = Person('Name', 10)
print person.name
print person.age
person.yolo = 'kitten'
print person.yolo
Download as text