[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Instance variables

Justin To

6/12/2008 4:07:00 PM

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!!
--
Posted via http://www.ruby-....

5 Answers

Avdi Grimm

6/12/2008 4:18:00 PM

0

On Thu, Jun 12, 2008 at 12:06 PM, Justin To <tekmc@hotmail.com> 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:...
Developer Blog: http:.../devblog/
Twitter: http://twitte...
Journal: http://avdi.livej...

Kevin Compton

6/12/2008 5:01:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

>
> # ??? 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

Avdi Grimm

6/12/2008 5:27:00 PM

0

On Thu, Jun 12, 2008 at 1:00 PM, Kevin Compton <klcompt@gmail.com> 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 :-)

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

--
Avdi

Home: http:...
Developer Blog: http:.../devblog/
Twitter: http://twitte...
Journal: http://avdi.livej...

Justin To

6/12/2008 5:32:00 PM

0

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

Thanks!
--
Posted via http://www.ruby-....

Robert Dober

6/12/2008 6:27:00 PM

0

On Thu, Jun 12, 2008 at 7:32 PM, Justin To <tekmc@hotmail.com> 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