-
Notifications
You must be signed in to change notification settings - Fork 0
/
improvement_1.rb
50 lines (39 loc) · 1.05 KB
/
improvement_1.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
39
40
41
42
43
44
45
46
47
48
49
class TelcoProvider
attr_accessor :provider_name
PROVIDERS = [
{ name: :maxis, homepage: 'maxis.com.my', email: '[email protected]', telephone_number: '03-12345678' },
{ name: :digi, homepage: 'digi.com.my', email: '[email protected]', telephone_number: '03-12344321' },
{ name: :celcom, homepage: 'celcom.com.my', email: '[email protected]', telephone_number: '03-87654321' }
]
def initialize(provider_name)
@provider_name = provider_name
end
def email
provider[:email]
end
def homepage
provider[:homepage]
end
def telephone_number
provider[:telephone_number]
end
PROVIDERS.each do |provider|
define_method("is_#{provider[:name]}?") do
provider_name == provider[:name]
end
end
private
def provider
@provider ||= begin
PROVIDERS.select do |provider|
provider_name == provider[:name]
end.first
end
end
end
telco = TelcoProvider.new(:maxis)
telco.telephone_number # =>
telco.homepage # =>
telco.email # =>
telco.is_maxis? # =>
telco.is_digi? # =>