Instance variables

class Trie
attr_reader :value, :parent, :child_node
def initialize(value=nil, parent=nil)
@value = value
@children = {}
@parent = parent
@child_node = @children # ??? HERE
end
end

??? HERE: Does it matter if I do @child_node = children (without the @

for children)? What’s the difference with using the @ and not?

Thank you!!

On Thu, Jun 12, 2008 at 12:06 PM, Justin To [email protected] wrote:

??? HERE: Does it matter if I do @child_node = children (without the @

for children)? What’s the difference with using the @ and not?

Since you haven’t defined an attr_reader for @children, you have to
use the instance variable (@).


Avdi

Home: http://avdi.org
Developer Blog: Avdi Grimm, Code Cleric
Twitter: http://twitter.com/avdi
Journal: http://avdi.livejournal.com

??? HERE: Does it matter if I do @child_node = children (without the @

for children)? What’s the difference with using the @ and not?

It depends on your intent with the variable children. If it is just a
temporary variable that you want to use while generating the @child_node
value then it is fine not to have the ‘@’

Kevin

On Thu, Jun 12, 2008 at 1:00 PM, Kevin C. [email protected]
wrote:

It depends on your intent with the variable children. If it is just a
temporary variable that you want to use while generating the @child_node
value then it is fine not to have the ‘@’

Hi Kevin :slight_smile:

Good catch. I think I missed the point of the OP’s question.


Avdi

Home: http://avdi.org
Developer Blog: Avdi Grimm, Code Cleric
Twitter: http://twitter.com/avdi
Journal: http://avdi.livejournal.com

Great, thanks. So what if I DID specify attr_reader :children, but still
did @child_node = @children. Would that have any adverse effects?

Thanks!

On Thu, Jun 12, 2008 at 7:32 PM, Justin To [email protected] wrote:

Great, thanks. So what if I DID specify attr_reader :children, but still
did @child_node = @children. Would that have any adverse effects?
No the parser identifies @children as an instance variable.
R