Coming from Java, I am trying to implement LinkedList in Ruby. The usual way I would implement this in Java is have an class called LinkedList and private inner class called Node with each object of LinkedList as Node object.
class LinkedList
private
class Node
attr_accessor :val, :next
end
end
I do not want to expose the Node class to external world. However with this setup in Ruby, I can access the private Node class object outside the LinkedList class using this -
node = LinkedList::Node.new
I know, with Ruby 1.9, we can use private_constant method to designate Node as private constant. But I am wondering if this is the right way to accomplish this? Also why am I able to create Node objects outside the LinkedList class even though it is declared as private?