3
votes

I had a test suite( hellow_world_test.rb) which has the following

#!/usr/bin/env ruby
begin
  gem 'minitest', '>= 5.0.0'
  require 'minitest/autorun'
  require_relative 'hello_world'
rescue Gem::LoadError => e
  puts "\nMissing Dependency:\n#{e.backtrace.first} #{e.message}"
  puts 'Minitest 5.0 gem must be installed for the xRuby track.'
rescue LoadError => e
  puts "\nError:\n#{e.backtrace.first} #{e.message}"
  puts DATA.read
  exit 1
end

# Test data version:
# deb225e Implement canonical dataset for scrabble-score problem (#255)

class HelloWorldTest < Minitest::Test
  def test_no_name
    assert_equal 'Hello, World!', HelloWorld.hello()
  end

  def test_sample_name 
    assert_equal 'Hello, Alice!', HelloWorld.hello('Alice')
  end

  def test_other_sample_name
    assert_equal 'Hello, Bob!', HelloWorld.hello('Bob')
  end
end

__END__

On the basis of that I had implemented the code like below

class HelloWorld
    def self.hello
        "Hello, World!"
    end

    def self.hello(named)
        "Hello, #{named}!"
    end
end

And right now my two test cases are passing and the first test case is returning

ArgumentError: wrong number of arguments (given 0, expected 1) /Users/vijay/Codes/Ruby/ruby/hello-world/hello_world.rb:7:in hello' hello_world_test.rb:21:intest_no_name'

Can some body help me on this?

1

1 Answers

5
votes

There is no method overloading by params in Ruby. With your second definition of hello you overwrote the first one. Anyway, you can obtain the same behaviour with just one method with a default value for the parameter.

def self.hello(named = 'World')
  "Hello, #{named}!"
end