How to do the wrong thing the right way ;-)

Posted by pratik
on Tuesday, August 14

Ruby never stops to surprise me.

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
class Object
  def _(method, *args)        
    if self.class.private_instance_methods.include?(method.to_s)
      send(method, *args)
    else
      raise "Stop acting like hasslehoff!!"
    end
  end
end

class Hello
  def hey
    puts "New world order!"
  end                    
  
  private
  
  def secret(foo, bar)
    puts "#{foo} is not #{bar}"
  end
  
  def hasslehoff
    puts "acts_as_hasslehoff ftw!"
  end
    
end

h = Hello.new
h._ :secret, "hello", "world" 
h._ :hasslehoff
h._ :hey, "srsly?" rescue puts "yay!" 

Call the private methods all you want. But at least do it the right way ;-)

Comments

Leave a response