Skip to content
MichaelWhi edited this page Oct 18, 2012 · 6 revisions

1) String functions, first try

Write a very basic function that checks whether a given String is a palindrome. An example for a palindrome is the name "Anna", because it is the same name read backwards. For this task we assume that strings only contain lower and upper case letters and spaces (no special characters or new lines, etc.)

First, simply write the body of the following function to test our example:

def palindrome?(word)

  # your method here

end

Note, that we end the method name with a question mark (?). This is Ruby convention for methods that return a boolean value. So, return either true or false instead of, for example, the string "yes". Remember, you do not explicitly have to call return – methods and functions, and actually all so called blocks, return the last statement/expression in its body.

Expected output

puts palindrome?("Anna") # => true

puts palindrome?("tattar rattat") # => true

puts palindrome?("Hello") # => false

Help:

Take a look at the methods you already have available in the Ruby String class: http://www.ruby-doc.org/core-1.9.3/String.html. With these, you'll be able to do the code without a for-loop.

(Another hint, don't take a look if you first wanna figure it out for yourself:

The methods sub (or maybe gsub? ;) ) might help you to get rid of spaces. Reversing a string is no hardwork in ruby, check out the reverse method. There are also methods to upcase or downcase a given string.)

2) String functions, monkey-patching

You probably called methods directly on a string in the task before, like someString.reverse, where someString is a concrete instance of a String like "Anna"

Wouldn't it be nice if we'd call "Anna".palindrome? and get true or false, instead of passing the String as an argument to a function?

In Ruby we can do this by simply pretending to write a String class just for the functions we want to implement (or even override, although this can be dangerous. Can you imagine why?). Defining a class for an existing class simply extends (or replaces in the case of overriding) the existing class by what you define.

So, lets start by defining a String class and adding our method. Fill in the body, again, and try out your function!

class String
  def palindrome?
    # your body here
  end
end

Note for Java developers

This is probably hard to do in Java, since you cannot mess around in the language core-classes. Probably you would end up using something like new SuperString("Anna") everywhere. However, some other languages support this kind of extending core classes (since, like here in ruby, they are basically just plain objects, as everything). Doing that kind of stuff is called monkey-patching.

Test your code

Instead of just calling puts and manually inspect the output, we can write a simply test, that checks the function with a few test strings. (Although we do not want to cover testing here, please note that testing in general is a very good idea, but maybe not achieving 100% test coverage and testing every single (and trivial) function.)

See the file https://github.com/MichaelWhi/PRubyTutorial/blob/master/problems/problem1.rb for an automatic test suite. It will run directly when you run the file (using your IDE or ruby problem1.rb)

We will get to minitest later, again, but if you're curious: It is a small testing framework, which is - next to others - included in Ruby. We use the MiniTest::Unit module, maybe comparable to a small JUnit.

Answer

A possible and very basic answer, that passes the tests, can be found in https://gist.github.com/3913904, but do not check that out until you're done.

Clone this wiki locally