Ruby Violates OO

Posted July 29, 2009

So this is fun - found another thing about Ruby that I find completely screws up a fairly basic principle of object-oriented design.

The whole point of access level control is to explicitly prevent access to certain methods under certain conditions, thereby allowing you to refactor your classes to put small common processes into methods which shouldn't be exposed. In other words, a "private" method should not be accessible by anything other than members of the same class. In Ruby, this is a complete farce. There's literally no reason to mark a method as private.

Let's take this code:

class Greeting
  def greet
    puts "Hello!"
  end
  private :greet
end

One would suspect you cannot access the greet method. WRONG!! Here's 2 things you can do without even breaking a sweat

x = Greeting.new
x.send :greet

Even easier ... it's even transparent once you apply it (you can invoke the method as normal):

x = Greeting.new
def x.greet
  super
end

And there you have it. Access levels are a joke!

Comments

There are no comments for this post.

No comments found

Add comment

Comments for this post have been disabled.

More Ruby and Rails Haters

Not everyone gets sucked into the hype of Ruby and Rails - here's some bashers, and some with very valid points.

nathan.crause.name

blog.taragana.com

cbcg.net

www.webdesignforums.net

glyphobet.net

toometa.com

discuss.joelonsoftware.com

www.sambal.org

www.cftopper.com

www.straw-dogs.co.uk

www.javalobby.org