在 `initialize` 方法之外声明一个实例变量
Declaring an instance variable outside of `initialize` method
我被教导用 def initialize
声明我的实例变量。我的印象是我只能在 initialize
方法中声明实例变量。
尽管如此,我在 initialize
方法之外声明了一个实例变量 @foo
,并使其按预期工作:
class FooBar
def initialize(bar)
@bar = bar
end
def foo_as_instance_var
@foo = @bar.split(' ')
@foo
end
end
x = "something wicked this way comes"
y = FooBar.new(x)
puts y.foo_as_instance_var
为什么我可以在 initialize
方法之外声明一个实例变量?由于我可以在任何方法中声明实例变量,是否有我应该遵循的最佳实践规则,关于在哪里声明实例变量(即在 initialize
内声明它们)还是无关紧要?
I have been taught to declare my instance variables with def initialize
由于 initialize
是对象生命周期中的第一个实例方法调用,您通常会在那里声明实例变量,以确保正确初始化变量。这也是我希望在阅读代码时首先定义实例变量的地方。
I have been under the impression that I could declare instance variables only within my initialize methods.
没有这样的限制。您可以在实例中的任何位置声明实例变量。
常见的用法是memoization:
class FooBar
def foo
@foo ||= expensive_operation
end
end
在第一次调用时,这将评估 expensive_operation
并将结果分配给 @foo
。在后续调用中,返回 @foo
。
另一个流行的示例是 Rails,它使用实例变量将数据从控制器传递到它的视图:
class FooController < ApplicationController
def index
@foos = Foo.all
end
end
is there a best practices rule I should follow, regarding where to declare instance variables
这取决于他们的目的(见上面的例子)。作为一般规则,以避免未定义变量(nil
错误)的方式声明它们并构建代码以便于阅读/遵循。
只是为了补充 Stefan 的出色回答
I have been taught to declare my instance variables with def initialize
ruby 新手常犯的错误是这样的:
class Person
@name = "John"
def introduce
puts "Hi, my name is #{@name}"
end
end
然后他们想知道为什么不打印他们的名字。为了使这项工作有效,可以在初始化程序中设置变量 @name
,就像指令中所说的那样。
让我们从最大的误称开始 - 在 Ruby 中没有声明变量的单独步骤 - 变量是在你设置它们时声明的。
有什么区别?看Java例如:
public class Bicycle {
private int cadence;
private int gear;
private int speed;
public Bicycle(int startCadence, int startSpeed, int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}
}
我们必须先声明所有实例变量,然后才能在初始化程序 (Bicycle) 中设置它们。 Ruby 中的相同代码为:
class Bicycle
def initialize(cadence, speed, gear)
@cadence = cadence
@speed = speed
@gear = gear
end
end
没有声明 - 只有赋值。 Ruby 甚至可以让您访问没有错误设置的实例变量。
irb(main):003:0> @not_set
=> nil
您不能(通常)在必须定义变量的语言中这样做*。
I have been taught to declare my instance variables with def
initialize. I have been under the impression that I could declare
instance variables only within my initialize methods.
废话。你可以在任何地方分配实例变量。它通常用于从 setter 和 mutator(改变对象的方法)到工厂方法(class 方法 return 一个实例)或任何你改变对象状态的地方。
class Book
def initialize(title, author)
@title = title
self.author = author # calls the setter.
end
# A factory method
def create_from_csv(filename)
# ...
end
# A very contrived setter
def author=(author)
@author = "#{author.forename.upcase}. #{author.surname}"
end
# a mutator
def out_of_print!
@out_of_print = true
@last_printed = Date.today
end
end
然而,initialize
方法是您应该处理初始化对象的地方(duuh),因此显然是设置初始值的地方。
我被教导用 def initialize
声明我的实例变量。我的印象是我只能在 initialize
方法中声明实例变量。
尽管如此,我在 initialize
方法之外声明了一个实例变量 @foo
,并使其按预期工作:
class FooBar
def initialize(bar)
@bar = bar
end
def foo_as_instance_var
@foo = @bar.split(' ')
@foo
end
end
x = "something wicked this way comes"
y = FooBar.new(x)
puts y.foo_as_instance_var
为什么我可以在 initialize
方法之外声明一个实例变量?由于我可以在任何方法中声明实例变量,是否有我应该遵循的最佳实践规则,关于在哪里声明实例变量(即在 initialize
内声明它们)还是无关紧要?
I have been taught to declare my instance variables with def initialize
由于 initialize
是对象生命周期中的第一个实例方法调用,您通常会在那里声明实例变量,以确保正确初始化变量。这也是我希望在阅读代码时首先定义实例变量的地方。
I have been under the impression that I could declare instance variables only within my initialize methods.
没有这样的限制。您可以在实例中的任何位置声明实例变量。
常见的用法是memoization:
class FooBar
def foo
@foo ||= expensive_operation
end
end
在第一次调用时,这将评估 expensive_operation
并将结果分配给 @foo
。在后续调用中,返回 @foo
。
另一个流行的示例是 Rails,它使用实例变量将数据从控制器传递到它的视图:
class FooController < ApplicationController
def index
@foos = Foo.all
end
end
is there a best practices rule I should follow, regarding where to declare instance variables
这取决于他们的目的(见上面的例子)。作为一般规则,以避免未定义变量(nil
错误)的方式声明它们并构建代码以便于阅读/遵循。
只是为了补充 Stefan 的出色回答
I have been taught to declare my instance variables with
def initialize
ruby 新手常犯的错误是这样的:
class Person
@name = "John"
def introduce
puts "Hi, my name is #{@name}"
end
end
然后他们想知道为什么不打印他们的名字。为了使这项工作有效,可以在初始化程序中设置变量 @name
,就像指令中所说的那样。
让我们从最大的误称开始 - 在 Ruby 中没有声明变量的单独步骤 - 变量是在你设置它们时声明的。
有什么区别?看Java例如:
public class Bicycle {
private int cadence;
private int gear;
private int speed;
public Bicycle(int startCadence, int startSpeed, int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}
}
我们必须先声明所有实例变量,然后才能在初始化程序 (Bicycle) 中设置它们。 Ruby 中的相同代码为:
class Bicycle
def initialize(cadence, speed, gear)
@cadence = cadence
@speed = speed
@gear = gear
end
end
没有声明 - 只有赋值。 Ruby 甚至可以让您访问没有错误设置的实例变量。
irb(main):003:0> @not_set
=> nil
您不能(通常)在必须定义变量的语言中这样做*。
I have been taught to declare my instance variables with def initialize. I have been under the impression that I could declare instance variables only within my initialize methods.
废话。你可以在任何地方分配实例变量。它通常用于从 setter 和 mutator(改变对象的方法)到工厂方法(class 方法 return 一个实例)或任何你改变对象状态的地方。
class Book
def initialize(title, author)
@title = title
self.author = author # calls the setter.
end
# A factory method
def create_from_csv(filename)
# ...
end
# A very contrived setter
def author=(author)
@author = "#{author.forename.upcase}. #{author.surname}"
end
# a mutator
def out_of_print!
@out_of_print = true
@last_printed = Date.today
end
end
然而,initialize
方法是您应该处理初始化对象的地方(duuh),因此显然是设置初始值的地方。