I saw the following program in Beginning Ruby, and can not figure out the purposes of set_first_name(first_name)
and set_last_name(last_name)
and why the two methods set_first_name
and set_last_name
use the same argument name
, because I think that name includes first_name
and last_name
.
我在Beginning Ruby中看到了以下程序,并且无法弄清楚set_first_name(first_name)和set_last_name(last_name)的用途以及为什么两个方法set_first_name和set_last_name使用相同的参数名称,因为我认为该名称包含first_name和last_name。
class Person
def initialize(name)
set_name(name)
end
def name
@first_name + ' ' + @last_name
end
def set_name(name)
first_name, last_name = name.split(/\s+/)
set_first_name(first_name)
set_last_name(last_name)
end
def set_first_name(name)
@first_name = name
end
def set_last_name(name)
@last_name = name
end
end
p = Person.new("Fred Bloggs")
puts p.name
1 个解决方案
#1
0
Ugh. That is a horrible introduction. There's a fair amount of magic that goes into determining if "name" is a local variable or method. And depending on what you do, you can switch it from one to the other.
啊。这是一个可怕的介绍。确定“名称”是否是局部变量或方法有相当多的魔力。根据您的操作,您可以将其从一个切换到另一个。
Add the following method to your class:
将以下方法添加到您的类:
def test
puts name
name = "Bob"
puts name
puts self.name
end
Then run it like this:
然后像这样运行:
p = Person.new('Jane Doe')
p.test
You'll get the following output:
您将获得以下输出:
Jane Doe
Bob
Jane Doe
What's happening is that at first name
isn't found as a local variable so it looks for a method (and will look up through the ancestors/modules of the class as well if there are any). Then we assign a value to name. Which makes my head hurt, but is legal. On the next puts name
now refers to that local variable. We can get back to the method by specifying self.name
.
发生的事情是,在第一个名称找不到局部变量,所以它寻找一个方法(并且如果有的话,将查找该类的祖先/模块)。然后我们为name分配一个值。这让我头疼,但是合法。在下一个put名称现在引用该局部变量。我们可以通过指定self.name来返回该方法。
Honestly, as the others have noted this class is poorly named (heh, pun intended? :). It's best to avoid this confusion if you can.
老实说,正如其他人已经注意到这个级别的名字很差(嘿,双关语打算?:)。如果可以的话,最好避免这种混乱。
David Black has a book called The Well-Grounded Rubyist. Chapter 5 in particular goes over this in great detail. It's a great book in general. No affiliation just a happy customer.
大卫布莱克有一本名为The Well-Grounded Rubyist的书。第5章特别详细介绍了这一点。这是一本很棒的书。没有从属关系只是一个快乐的客户。
http://www.manning.com/black2/
http://www.manning.com/black2/
#1
0
Ugh. That is a horrible introduction. There's a fair amount of magic that goes into determining if "name" is a local variable or method. And depending on what you do, you can switch it from one to the other.
啊。这是一个可怕的介绍。确定“名称”是否是局部变量或方法有相当多的魔力。根据您的操作,您可以将其从一个切换到另一个。
Add the following method to your class:
将以下方法添加到您的类:
def test
puts name
name = "Bob"
puts name
puts self.name
end
Then run it like this:
然后像这样运行:
p = Person.new('Jane Doe')
p.test
You'll get the following output:
您将获得以下输出:
Jane Doe
Bob
Jane Doe
What's happening is that at first name
isn't found as a local variable so it looks for a method (and will look up through the ancestors/modules of the class as well if there are any). Then we assign a value to name. Which makes my head hurt, but is legal. On the next puts name
now refers to that local variable. We can get back to the method by specifying self.name
.
发生的事情是,在第一个名称找不到局部变量,所以它寻找一个方法(并且如果有的话,将查找该类的祖先/模块)。然后我们为name分配一个值。这让我头疼,但是合法。在下一个put名称现在引用该局部变量。我们可以通过指定self.name来返回该方法。
Honestly, as the others have noted this class is poorly named (heh, pun intended? :). It's best to avoid this confusion if you can.
老实说,正如其他人已经注意到这个级别的名字很差(嘿,双关语打算?:)。如果可以的话,最好避免这种混乱。
David Black has a book called The Well-Grounded Rubyist. Chapter 5 in particular goes over this in great detail. It's a great book in general. No affiliation just a happy customer.
大卫布莱克有一本名为The Well-Grounded Rubyist的书。第5章特别详细介绍了这一点。这是一本很棒的书。没有从属关系只是一个快乐的客户。
http://www.manning.com/black2/
http://www.manning.com/black2/