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.
attr_accessor :val, :next
node = LinkedList::Node.new
why am I able to create Node objects outside the LinkedList class even though it is declared as private?
Because in ruby constants ignore "regular" visibility modifiers. They're always public, regardless of which section they're in. To make them private, use
private_constant. Call this inelegant design or whatever, but that's how it is.
Also, be warned that even with
private_constant, the privateness means very little. Basically, the only thing it does is hide the constant from lists (
LinkedList.constants) and direct resolution (
LinkedList::Node). If one knows the name, they will be able to access it.
class LinkedList class Node attr_accessor :val, :next end private_constant :Node end LinkedList.const_get('Node') # => LinkedList::Node