-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperson.rb
37 lines (32 loc) · 947 Bytes
/
person.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
class Person
attr_accessor :name, :age, :gender, :height, :weight
def initialize(name, age, gender, height, weight)
@name = name
@age = age
@gender = gender
@height = height
@weight = weight
# automatically creating singleton methods
create_method(name)
def method_missing(method_name, *args, &block)
if method_name.to_s.include?('summary')
# dynamically creating methods
puts "This is the summary of person named: #{name}"
else
puts "#{method_name} is missing..."
end
end
end
# creating singleton method so we can use during object initialization
def create_method(name)
define_singleton_method("#{name}s_bio") do |args|
"This is my bio with args #{args}"
end
end
# defining methods manually
# def self.create_method(name)
# define_method("#{name}s_bio") do |args|
# "This is my bio with args #{args}"
# end
# end
end