· 7 years ago · Jun 08, 2018, 05:24 AM
1We can call private method in different ways.
2
31. instance_eval
42. send method
53. Call private method from public method
6
7---------------------------------
8# class example
9class User
10 def initialize(email)
11 @email = email
12 end
13
14 private
15 def secret_key
16 'XX-XXXX-XXXX-XXXX'
17 end
18end
19---------------------------------
20u = User.new('ruby@devscoop.fr')
21
22Access methods
231. instance_eval
24 => u.instance_eval('secret_key') # returns: "XX-XXXX-XXXX-XXXX"
252. Send method
26 => u.send('secret_key') # returns: "XX-XXXX-XXXX-XXXX"