Lab 5: Exploring Objects and Methods

Objectives

  • Know how to invoke methods on objects
  • Use methods to obtain information about objects
  • Use methods to perform operations on objects
  • Understand how to look up and read documentation for classes

Objects and Methods

This week in lecture we went over a number of different ways by which you could discover the methods supported by given Ruby objects. In this lab you’ll write a few programs and do a little bit of research to show that you can make use of the resources we discussed.

Lab exercise 1

Write a program that prints out all the methods supported by String objects.

Lab exercise 2

Look through the documentation for the String class (this is easiest via http://www.ruby-doc.org/) and make a list of 5 methods. For each of these methods you should come up with the following:

  • a description, in your own words, of what it is used for
  • a short program to demonstrate its effect on an actual String (note that you can combine your programs for the various methods into one file, if you wish)

Lab exercise 3

Complete exercise 2 again, except this time for the Fixnum class. You may not use the methods +, -, /, or * in your solution.

Leave a comment

You must be logged in to post a comment.