Ruby 从数组中的对象中删除实例变量
我是Ruby新手,我只是在玩弄一些想法,我想做的是从我创建的country_数组中删除@Continental数据。做了大量的搜索,可以找到关于整体删除元素的大量信息,但找不到如何具体删除@Continental数据。请保持任何答案相当简单,因为我是新的,但任何帮助非常感谢Ruby 从数组中的对象中删除实例变量,ruby,Ruby,我是Ruby新手,我只是在玩弄一些想法,我想做的是从我创建的country_数组中删除@Continental数据。做了大量的搜索,可以找到关于整体删除元素的大量信息,但找不到如何具体删除@Continental数据。请保持任何答案相当简单,因为我是新的,但任何帮助非常感谢 class World include Enumerable include Comparable attr_accessor :continent def <=> (sorted) @
class World
include Enumerable
include Comparable
attr_accessor :continent
def <=> (sorted)
@length = other.continent
end
def initialize(country, continent)
@country = country
@continent = continent
end
end
a = World.new("Spain", "Europe")
b = World.new("India", "Asia")
c = World.new("Argentina", "South America")
d = World.new("Japan", "Asia")
country_array = [a, b, c, d]
puts country_array.inspect
[#<World:0x100169148 @continent="Europe", @country="Spain">,
#<World:0x1001690d0 @continent="Asia", @country="India">,
#<World:0x100169058 @continent="South America", @country="Argentina">,
#<World:0x100168fe0 @continent="Asia", @country="Japan">]
阶级世界
包括可枚举的
包括可比
attr_存取器:大陆
def(已排序)
@长度=其他长度
结束
def初始化(国家、大陆)
@国家
@大陆
结束
结束
a=世界新(“西班牙”、“欧洲”)
b=世界新(“印度”、“亚洲”)
c=世界新(“阿根廷”、“南美”)
d=世界新(“日本”、“亚洲”)
国家/地区数组=[a、b、c、d]
将国家/地区设置为数组
[#,
#,
#,
#]
以下示例将为数组中的第一个世界
对象将@大陆
设置为nil
:
country_array[0].continent = nil
irb(main):035:0> country_array[0]
=> #<World:0xb7dd5e84 @continent=nil, @country="Spain">
country\u数组[0]。大陆=nil
irb(主):035:0>国家/地区阵列[0]
=> #
但它并没有真正删除大陆变量,因为它是世界
对象的一部分
你在面向对象编程方面做过很多工作吗?你的World
示例是否来自某个地方的书籍或教程?我建议对您的世界的结构进行一些更改。一个世界
可以有一个大陆
的数组,每个大陆
可以有一个国家
的数组
名称具有含义,变量名称应反映其真实含义。country\u数组
变量可以重命名为world\u数组
,因为它是world
对象的数组。您可以使用删除实例变量
。但是,由于它是一个私有方法,因此需要重新打开类并添加一个新方法来执行此操作:
class World
def remove_country
remove_instance_variable(:@country)
end
end
然后你可以这样做:
country_array.each { |item| item.remove_country }
# => [#<World:0x7f5e41e07d00 @country="Spain">,
#<World:0x7f5e41e01450 @country="India">,
#<World:0x7f5e41df5100 @country="Argentina">,
#<World:0x7f5e41dedd10 @country="Japan">]
country_数组.each{| item | item.remove_country}
# => [#,
#,
#,
#]
99%的时候我建议不要删除实例变量,因为它是额外的代码,没有额外的好处
当您编写代码时,通常您是在试图解决现实世界中的问题。对于实例变量,要问的问题有:
- 我试图用变量可能处于的各种状态来模拟什么样的现实世界概念
- 我将如何处理存储在变量中的值
如果您只是试图清空存储在World
对象中的大陆值,您可以按照dustmachine的说明将@contraction
设置为nil
。这将适用于99%的情况。(访问已删除的实例变量只会返回nil
)
删除实例变量时唯一可能有用的情况(我能想到)是缓存可能为nil
的值。例如:
class Player
def score(force_reload = false)
if force_reload
# purge cached value
remove_instance_variable(:@score)
end
# Calling 'defined?' on an instance variable will return false if the variable
# has never been set, or has been removed via force_reload.
if not defined? @score
# Set cached value.
# Next time around, we'll just return the @score without recalculating.
@score = get_score_via_expensive_calculation()
end
return @score
end
private
def get_score_via_expensive_calculation
if play_count.zero?
return nil
else
# expensive calculation here
return result
end
end
end
由于nil
是@score
的一个有意义的值,因此我们不能使用nil
来表示尚未缓存该值。因此,我们使用未定义状态来告诉我们是否需要重新计算缓存值。因此,@score
有3种状态:
nil
(表示用户未玩过任何游戏)
数字(表示用户至少玩过一次,但未累积任何分数)
未定义(表示我们尚未获取玩家对象的计算分数)
现在确实可以使用另一个不是数字的值来代替未定义的状态(例如像:unset
这样的符号),但这只是一个人为的例子来演示这个想法。在某些情况下,您的变量可能包含未知类型的对象。为什么在
的定义中有一个未使用的变量进行排序
?,以及forsother
的来源?我是一个初学者,正在学习一本书,尽管这是一种尝试,但我已经学到了一些东西,谢谢你的回答。谢谢,这很好,也与我目前的学习很好地联系在一起,非常感谢!删除实例变量不是我喜欢做的事情。我不太愿意向编程新手推荐这种东西(至少在没有警告信息的情况下)。当我们尝试访问country\u数组[0]时,代码中后面的位置会发生什么情况?如果它已被删除,大陆
?@dustmachine我同意您的一般看法,但请注意,对于现有的类,没有。大陆
访问器方法,因此,与当前发生的情况相同:NoMethodError
:)有一行attr\u accessor:containment
,但这可能是在您的注释之后进行的编辑。您应该采取防御措施,并添加一个guard子句来检查是否定义了实例变量。如果不这样做,您将得到一个错误“实例变量…未定义”<代码>删除实例变量(:@country)(如果定义了实例变量)(:@country)