[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

[Newbie] Question about instance method scoping/parsing (?

Chris Gernon

1/5/2007 10:38:00 PM

Can anyone explain to me what's going on here?

-------------------------------------
#!/usr/bin/env ruby -w

class Thingy
attr_accessor :name

def test
puts "self.name: #{self.name}"
puts "name: #{name}"

name ||= 'default value'

puts "self.name: #{self.name}"
puts "name: #{name}"
end
end

thing1 = Thingy.new
thing1.name = 'custom value'
thing1.test
-------------------------------------
$ ruby dditest.rb
self.name: custom value
name: custom value
self.name: custom value
name: default value
-------------------------------------

I thought calling a method without a receiver (i.e. name, name=) within
an instance method would always call that method on the current object.
Is this not true for setters (i.e. Thingy#name=)?

Additionally, why is the first instance of name interpreted as a method
call and the last instance interpreted as (I think) a local variable?

Thanks,
Chris

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

2 Answers

dblack

1/5/2007 10:46:00 PM

0

Galen Hekhuis

8/31/2010 6:01:00 PM

0

On Tue, 31 Aug 2010 12:36:06 -0500, "(?`?.?Craig Chilton?.???)
<http://www.ChristianEgalitarian.com... <x@v.com> wrote:

>
> RESOURCE Post
>
> === AS a Resource Post, this will be refreshed at
> regular intervals to remind Readers to keep
> pressure on the President and Congress to
> PASS this modification, and then send it to the
> states for ratification. ===
>
>
> A **GOOD** Idea Whose Time Has Come!
> MODIFY the 14th Amendment
> to PROHIBIT "Anchor Baby" Citizenship!

What about "temporary mooring babies"?

Darn! I thought at least this country (unlike many others) didn't
care who your parents were. What's next, DNA tests?