2013-02-17 135 views
2

我有这个测试在Ruby中,我试图实现 要求“silly_blocks”实现问题

describe "some silly block functions" do 

    describe "reverser" do 
    it "reverses the string returned by the default block" do 
     result = reverser do 
     "hello" 
     end 
     result.should == "olleh" 
    end 

    it "reverses each word in the string returned by the default block" do 
     result = reverser do 
     "hello dolly" 
     end 
     result.should == "olleh yllod" 
    end 
    end 

的方法

def do_reverse(str) 
str = str.split 
first_str = str[0].reverse 
second_str= str[1] 
if (second_str == nil) 
    str = first_str.to_s 
else 
second_str = str[1].reverse 
str = (first_str +" "+ second_str) 
end 
end 

什么是最好的方法我可以实现它。当我试图耙测试它失败,但方法本身返回储备。我只是有点困惑。

回答

1

这里有一个简单的方法来做你想要的东西,与规格。

# lib/reverse_words.rb 
def reverse_words(phrase) 
    return '' if phrase.nil? 
    words = phrase.split 
    phrase.split.map(&:reverse!).join(' ') 
end 

def reverser 
    reverse_words(yield) 
end 

# spec/reverse_words_spec.rb 
describe "#reverse_words" do 
    context "when single word" do 
    subject { reverse_words("hello") } 
    it { should == "olleh" } 
    end 

    context "when multiple words" do 
    subject { reverse_words("hello dolly") } 
    it { should == "olleh yllod" } 
    end 

    context "when nil" do 
    subject { reverse_words(nil) } 
    it { should == '' } 
    end 

    context "when empty" do 
    subject { reverse_words('') } 
    it { should == '' } 
    end 

end 

注意,reverser规范仅仅使用行为是reverse_words已经specced通过。

describe "#reverser" do 
    subject do 
    reverser do 
     "this is a test" 
    end 
    end 
    it { should == reverse_words("this is a test") } 
end 

这里的少罗嗦reverse_words规格:

describe "#reverse_words (less wordy)" do 
    # counterintuitive keys as answers to support the nil case 
    cases = { "olleh" => "hello", 
     "olleh yllod" => "hello dolly", 
     ''   => nil, 
     ''   => '' 
    } 

    cases.each_pair do |expected, input| 
    context "#{input} should equal #{expected}" do 
     subject { reverse_words(input) } 
     it { should == expected } 
    end 
    end 
end 
+0

是的,它很重要。我分享的规范不是由我完成的,他们给我实施方法 – 2013-02-17 19:23:16

+0

更新的答案,包括'reverser',它适用于块 – cfeduke 2013-02-17 19:38:01

0

这工作。你想要的数据存储在“yield”中。

def reverser 
    yield.gsub(/\w+/) { |w| w.each_char.to_a.reverse.join } 
end 
2

试试这个代码:

def reverser 

    yield.split.map { |word| word.reverse}.join(" ") 

end 
0

我反向方法:

def reverser 
    # yield is the string given in the block 
    words = yield.split(' ') 
    final = [] 
    words.each do |word| 
     final.push(word.reverse) 
    end 
    final.join(' ') 
end 
0

所以。我来到这里寻找如何做到这一点的信息。由于语言不清楚。我去了现场,发现了足够的信息来通过测试。

因此,块是花括号之间的那些事情,有时跟随功能红宝石,如

list.each {|i| i.reverse} 

那么规范正在做的是试图找出当它发生了什么:

rerverser {"hello"} 

在函数把产率只是返回无论是在块中,所以

def print_block 
    puts yield 
end 

print_block {"Hello world."} 

#=> "Hello world" 

然后你可以像操纵任何参数一样操纵yield。还有更多的块。 Here's a good place to start,但如果你已经完成了所有Test First的learn_ruby练习,那么这就是解决练习所需要知道的一切。