Question

Here is simon_says_spec.rb

require simon_says
     describe "repeat" do
        it "should repeat" do
          repeat("hello").should == "hello hello"
        end

        # Wait a second! How can you make the "repeat" method
        # take one *or* two arguments?
        #
        # Hint: *default values*
        it "should repeat a number of times" do
          repeat("hello", 3).should == "hello hello hello"
        end
      end
    end

And here is my simon_says.rb

def repeat(x,y)
        y.times do print x + ‘ ‘
    end
end

After i fixed def repeat(x,y = 2) When I run rake on window, it works just fine. But when i run rake on mac. This is what i got

/Users/thanhnguyen/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require': /Users/thanhnguyen/Downloads/learn_ruby-master/03_simon_says/simon_says.rb:10: syntax error, unexpected tIDENTIFIER, expecting keyword_do or '{' or '(' (SyntaxError)
/Users/thanhnguyen/Downloads/learn_ruby-master/03_simon_says/simon_says.rb:11: syntax error, unexpected end-of-input, expecting keyword_end
    from /Users/thanhnguyen/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /Users/thanhnguyen/Downloads/learn_ruby-master/03_simon_says/simon_says_spec.rb:14:in `<top (required)>'
    from /Users/thanhnguyen/.rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `load'
    from /Users/thanhnguyen/.rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `block in load_spec_files'
    from /Users/thanhnguyen/.rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `each'
    from /Users/thanhnguyen/.rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `load_spec_files'
    from /Users/thanhnguyen/.rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:22:in `run'
    from /Users/thanhnguyen/.rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:80:in `run'
    from /Users/thanhnguyen/.rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:17:in `block in autorun'
/Users/thanhnguyen/.rvm/rubies/ruby-2.1.1/bin/ruby -S rspec /Users/thanhnguyen/Downloads/learn_ruby-master/03_simon_says/simon_says_spec.rb -I/Users/thanhnguyen/Downloads/learn_ruby-master/03_simon_says -I/Users/thanhnguyen/Downloads/learn_ruby-master/03_simon_says/solution -f documentation -r ./rspec_config failed

Ive been struggling with this error for couple days but still cannot fix it. I run

`gem install spec` 

i run bundle install

This is my system

ruby 2.1.1
rails 4.1.1
rvm 1.25.25
bundler 1.6

Would somebody plz help?

Was it helpful?

Solution 2

In the below part, you passed only 1 argument instead 2.

describe "repeat" do
        it "should repeat" do
          repeat("hello").should == "hello hello"
          #        ^ why only 1 argument ?
        end

As per your code, it should be repeat("hello", 2).should == "hello hello".

As per commented hints, you can write also :-

def repeat(x, y = 2)
   y.times { print x + ‘ ‘ }
end

Now, the test code you wrote, will work without error, with above modified method definition.

require simon_says

describe "repeat" do
  it "should repeat" do
    # here you are not passsing the second argument. But *repeat* method has `2`
    # as its default value of the second argument. So no issue will be here.
    repeat("hello").should == "hello hello"
  end

  it "should repeat a number of times" do
    repeat("hello", 3).should == "hello hello hello"
  end
end

Read this default argument to know, how default argument works in Ruby.

OTHER TIPS

Picking up where @Arup left off (you've edited the question with a follow-up)

  1. you are using a non-ASCII char instead of ' - ruby does not like that.
  2. Your code does not return the chained text, but rather the number of times you multiplied it (it does print the result though)

    3.times do '123' end
    # => 3
    

    So you test will fail.
    A better way to do that will be

    y.times.map { x + ' ' }.join
    

And in a more ruby way:

([x] * y).join(' ')

You are missing the end statement for your do loop.

def repeat(x, y = 2)
    y.times do print x + ' ' end
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top