2016-05-05 16 views
1

クラスオブジェクトの配列を作成しようとしていますが、コードが機能しません。 Solution.newを作成するとnilが返され、各行の単語から配列の配列を返したいとします。test.txt 私はルビでクラスオブジェクトの配列を作成する方法

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 

のRuby 2.1.5を使用していると私は

foo = Solution.new 
foo.read_file 

を作るとき、今ではnilを返します。

+0

Rubyは、常にメソッドの2つのうちのどれかを返す: 'return'キーワードを使って何を伝えるのか、それともメソッドの最後の式の値に到達するのか。 –

+0

また、タイトルはあなたの質問とはまったく異なります。あなたの質問は、「なぜこのメソッドは配列の代わりにnilを返すのですか?」というようなものです。 –

答えて

0
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 

この '配列'行は、メソッド呼び出しから戻す必要があるため、追加する必要があります。また、ここでLineクラスを少し簡略化しました。基本的には、このコードでは問題を解決できますが、行の解析には正規表現を使用することを検討してください。

+0

このコードでは質問に答えるかもしれませんが、_why_および/または_how_に関する追加のコンテキストを に追加すると、 と回答すると、その質問は長期的には の値が大幅に改善されます。あなたの答えを[編集]して、説明を加えてください。 –

0

はこれを試してみてください:

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 
3

私はSolution.newはあなたの例でnilを返しているとは思わない、それは

(あなたの例ではfooを)ソリューションの新しいインスタンスを返しますあなたの主な問題は、そのread_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">] 

は空白で、それぞれの行を分割:

class Solution 
    def read_file 
    lines = [] 
    File.foreach('test.txt') do |line| 
     words = [] 
     line.strip.split(/\s+/).each do |word| 
     words << word 
     end 

     lines << Line.new(words) 
    end 

    lines 
    end 
end 

のキーラインコードはline.strip.split(/\s+/)で、最初に文字列の先頭と末尾の空白を取り除き、空白に基づいて文字列を分割して配列に変換します(/s+/正規表現は1つ以上の空白文字に一致しますs)。

いくつかの他の提案:あなたがしたい場合

は、デフォルトの引数を設定することができread_fileの引数としてファイル名を渡すために:

class Solution 
    def read_file(filename = 'test.txt') 
    array = [] 
    File.foreach(filename) do |line| 
     array << Line.new(line) 
    end 

    array 
    end 
end 

最後に、はるかにエレガントな解決のために、あなたmapを使用し、.splitを呼び出してネストされた配列を返すことができます。この場合、Lineクラスは実際にはあまり効果がありません。

class Solution 
    def read_file 
    File.foreach('test.txt').map do |line| 
     line.strip.split(/\s+/) 
    end 
    end 
end 

これは、単純に配列の配列を返します。内部配列には、各行の単語が含まれています。

0

ではなく、不要な変数を作成するEnumerable#injectを使用するように考えてみましょう:

class Solution 
    def read_file 
    File.foreach('test.txt').inject([]) do |memo, line| 
     memo << Line.new(line) 
    end 
    end 
end 

か、この特定のケースでは、mapは、トリックを行います:

class Solution 
    def read_file 
    File.foreach('test.txt').map &Line.method(:new) 
    end 
end 
0

あなたがする必要があるすべては、配列を取得している場合単語全体を一度にメモリにロードしても構いませんが、以下のコードで非常に簡単に行うことができます(word_arrays = ...で始まる3行、残りは設定と出力)。

#!/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 
関連する問題