本练习的额外信用问题:
问:为什么你 稍后调用变量
jelly_beans但名字beans?答:这是功能如何运作的一部分.请记住,在函数内部,变量是临时的.当您返回它时,可以将其分配给变量以供日后使用.我只是创建一个名为
beanshold的新变量 来保存返回值.
什么是"函数内部的变量是暂时的"是什么意思?这是否意味着该变量在return?之后无效?似乎在函数缩进之后,我无法打印函数部分中使用的变量.
从答案中可以看出"当你返回它时,它可以被分配给一个变量供以后使用".有人可以解释一下这句话吗?
print "Let's practice everything."
print 'You\'d need to know \'bout escape with \\ that do \n newlines and \t tabs.'
poem = """
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explanation
\n\t\twhere there is none.
"""
print "-------------"
print poem
print "-------------"
five = …Run Code Online (Sandbox Code Playgroud) 我不明白 struct 和 class 相等性检查之间的区别。由于 Struct 和 Class 都从内核获取 #hash,但它们的行为似乎不同。
我知道 instance.hash 会为每个类实例产生不同的结果。与类实例 [Foo, Object, Kernel, BasicObject] 相比,Struct 实例具有不同的祖先 [Customer, Struct, Enumerable, Object, Kernel, BasicObject]。是什么真正导致每个 Class 实例与其他实例具有不同的哈希数
Customer = Struct.new(:name, :phone, :address) do
end
class Foo
def initialize(the_name, phone, address)
@name = the_name
@phone = phone
@address = address
end
end
str_a = Customer.new('bond', 'ring', 'address')
str_b = Customer.new('bond', 'ring', 'address')
foo_a = Foo.new('bond', 'ring', 'address')
foo_b = Foo.new('bond', 'ring', 'address')
p str_a == str_b #true
p foo_a == foo_b …Run Code Online (Sandbox Code Playgroud)