Arrays 如何在ruby中创建类对象数组
我试图创建一个类对象数组,但我的代码不起作用。当我创建Solution.new时,它返回nil,我希望它从Arrays 如何在ruby中创建类对象数组,arrays,ruby,Arrays,Ruby,我试图创建一个类对象数组,但我的代码不起作用。当我创建Solution.new时,它返回nil,我希望它从test.txt的每一行中的单词返回一个数组。 我正在使用Ruby 2.1.5 class Line def initialize (content) @content = content self.line_arr end def line_arr @content.split end end class Solution def read_
test.txt
的每一行中的单词返回一个数组。
我正在使用Ruby 2.1.5
class Line
def initialize (content)
@content = content
self.line_arr
end
def line_arr
@content.split
end
end
class Solution
def read_file
array = []
File.foreach('test.txt') do |line|
array << Line.new(line)
end
end
end
它返回nil
请尝试以下操作:
class Line
def initialize (content)
@content = content
self.line_arr
end
def line_arr
@content.split
end
end
class Solution
def initialize
self.read_file
end
def read_file
array = []
File.foreach('test.txt') do |line|
array << Line.new(line)
end
array
end
end
类行
def初始化(内容)
@内容=内容
自连线
结束
def管路
@内容分割
结束
结束
类解决方案
def初始化
self.read\u文件
结束
def read_文件
数组=[]
File.foreach('test.txt')do |行|
数组我不认为解决方案。在您的示例中,new
返回nil
,它返回一个新的解决方案实例(foo
)
您的主要问题是read\u file
返回file.foreach
的值,该值始终为nil
首先,更新read_file
方法以返回数组本身:
class Solution
def read_file
array = []
lines = []
File.foreach('test.txt') do |line|
lines << Line.new(line)
end
array << lines
array
end
end
solution = Solution.new
solution.read_file
# outputs:
# [#<Line:0x007fab92163b50 @content="This Is A Line\n">, #<Line:0x007fab92161be8 @content="Line 2\n">, #<Line:0x007fab92160d88 @content="Line3">]
最后,为了获得更优雅的解决方案,您可以使用map
,只需调用.split
即可返回嵌套数组。在这种情况下,行
类实际上做的不多
class Solution
def read_file
File.foreach('test.txt').map do |line|
line.strip.split(/\s+/)
end
end
end
这将只返回一个数组数组,其中内部数组包含每行的单词。类行
class Line
attr_reader :content
def initialize (content)
@content = content.split(' ')
end
end
class Solution
def read_file
array = []
File.foreach('test.txt') do |line|
array << Line.new(line).content
end
array
end
end
属性读取器:内容
def初始化(内容)
@content=content.split(“”)
结束
结束
类解决方案
def read_文件
数组=[]
File.foreach('test.txt')do |行|
数组考虑使用,而不是创建不必要的变量:
class Solution
def read_file
File.foreach('test.txt').inject([]) do |memo, line|
memo << Line.new(line)
end
end
end
如果您只需要获取单词数组,并且不介意立即将整个文件加载到内存中,那么只需使用下面的代码即可完成(以word\u arrays=…
开头的3行代码,其余代码都已设置并输出):
#/usr/bin/env ruby
File.write('woods.txt',
“树林可爱、幽暗、幽深
但我要信守诺言
在我睡觉之前还有好几英里
在我睡觉之前还有好几英里呢
word_arrays=File.readlines('woods.txt')。每个带有_对象([])的_都有|行,word_数组|
word_arrays Ruby总是从方法中返回两件事之一:使用return
关键字告诉它什么,或者如果它到达了,则返回方法中最后一个表达式的值。此外,你的标题与你的问题完全不同。您的问题更像是“为什么此方法返回nil而不是数组?”尽管此代码可能会回答此问题,但提供有关为什么和/或如何回答此问题的附加上下文将显著提高其长期价值。请在您的回答中添加一些解释。
class Solution
def read_file
File.foreach('test.txt').map do |line|
line.strip.split(/\s+/)
end
end
end
class Line
attr_reader :content
def initialize (content)
@content = content.split(' ')
end
end
class Solution
def read_file
array = []
File.foreach('test.txt') do |line|
array << Line.new(line).content
end
array
end
end
class Solution
def read_file
File.foreach('test.txt').inject([]) do |memo, line|
memo << Line.new(line)
end
end
end
class Solution
def read_file
File.foreach('test.txt').map &Line.method(:new)
end
end
#!/usr/bin/env ruby
File.write('woods.txt',
"The woods are lovely, dark, and deep
But I have promises to keep
And miles to go before I sleep
And miles to go before I sleep")
word_arrays = File.readlines('woods.txt').each_with_object([]) do |line, word_arrays|
word_arrays << line.split
end
word_arrays.each.with_index do |words, index|
puts "#{index}: #{words} "
end
=begin
Prints:
0: ["The", "woods", "are", "lovely,", "dark,", "and", "deep"]
1: ["But", "I", "have", "promises", "to", "keep"]
2: ["And", "miles", "to", "go", "before", "I", "sleep"]
3: ["And", "miles", "to", "go", "before", "I", "sleep"]
=end