2016-03-25 8 views
0

のための未定義メソッド `split 'Ive beeは以下のタスクを設定し、タイトルに記載されているエラーを続けるが、その理由を理解できない。どんな助けも素晴らしいだろう。#<Enumerator:0x007fcb41399a90> Ruby

# Given a sentence, return an array containing every other word. 
# Punctuation is not part of the word unless it is a contraction. 
# In order to not have to write an actual language parser, there won't be any punctuation too complex. 
# There will be no "'" that is not part of a contraction. 
# Assume each of these charactsrs are not to be considered: ! @ $ # %^& * () - = _ + [ ] : ; , ./< > ? \ | 
# 
# Examples 
# alternate_words("Lorem ipsum dolor sit amet.") # => ["Lorem", "dolor", "amet"] 
# alternate_words("Can't we all get along?")  # => ["Can't", "all", "along"] 
# alternate_words("Elementary, my dear Watson!") # => ["Elementary", "dear"] 

def alternate_words(string) 

    no_p = string.gsub(/[^a-z ^A-Z ^0-9 ']/) 
    new_words = [] 

    no_p.split.each.with_index {|x,i| new_words << string[i] if i.even? } 

    new_words 
end 

答えて

1

no_p = string.gsub(/[^a-z ^A-Z ^0-9 ']/)あなたは正しくgsubを使用していません。置換文字は何ですか? http://ruby-doc.org/core-2.1.4/String.html#method-i-gsubを参照してください。置換を指定しない場合、gsubはEnumeratorオブジェクトを返します。

+0

私に戻ってくれてありがとう。私はgsubがa-z、A-Z、0-9と "'"からすべての文字を保持することを望んでいましたが、それ以外は何もありませんでした。 –

0

String.gsubは、単一の引数だけが渡されたときに列挙子を返します。 Stringを返すには、そのメソッドの置換またはブロックを指定する必要があります。 splitはStringのメソッドであり、Enumeratorではなくエラーです。

関連する問題