Tuesday, January 8, 2008

Struct

Question=Struct.new(:question, :answer)
=> Question
q1=Question.new("what's your first name?","subbu")
=> #
q1.question
(behaves like an object)
=> "what's your first name?"
q1[:question]
(behaves like a symbol)
=> "what's your first name?"
q1['question']
(behaves like a hash)
=> "what's your first name?"
q1[0]
(behaves like an array)
=> "what's your first name?"
---------------------------
This means, I can treat my Struct based on what the situation demands. Sometimes I can treat it like an array or a hash or an object or a symbol.

No comments: