Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class example Ruby
05-15-2010, 11:26 PM
Post: #1
Class example Ruby
This is a example of classes in Ruby with operator overloading.

Code:
=begin
This program is a Gradebook class example
coded by codecaine aka Jerome Scott II
=end
class GradeBook

  #default contrustor for GradeBook
  def initialize(instructor, course)
    @instructor_name = instructor #initialize instructor name
    @course_name = course #initialize course name
  end

  #set instructor name
  def instructor_name=(instructor)
    #check if the string is empty
      if not instructor.strip.empty?
        @instructor_name = instructor
    else
      puts 'Error: Tried to provide an empty string for instructor_name'
    end
  end

  #get instructor name
  def instructor
    @instructor_name
  end

  #set course name
  def course_name=(course)
    #check if the string is empty
      if not course.strip.empty?
        @course_name = course
    else
      puts 'Error: Tried to provide an empty string for course_name'
    end
  end

  #get course name
  def course_name
    @course_name
  end

  #display course instructor
  def display_message
    puts "This course is presented by: #{@instructor_name}"
  end

end

gb = GradeBook.new('codecaine','CIS 101') #initialize a gradebook
gb.course_name = '  ' #try to set a empty string for course name
puts gb.course_name #print course name
gb.display_message #print the cou
Visit this user's website Find all posts by this user
Quote this message in a reply
05-16-2010, 01:00 AM
Post: #2
RE: Class example Ruby
This is very good codecaine.
Back_track also made something about classes in Ruby here :
http://www.pro9ramming.com/-ruby-classes...-1261.html

We attack any new problem we encounter with techniques we already know, and try small modifications if difficulties turn up.
Bram Cohen - Bit Torrent Creator
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: