
-----------------------------------
JHanson90
Fri Aug 27, 2004 11:37 pm

Some Ruby Help
-----------------------------------
I know at least one of you knows Ruby, or some of it anyway.  I just started learning it from "Programming Ruby, The Pragmatic Programmer's Guide."  Right now it's teaching me about classes, specifically Inheritance.  Here's an excerpt of what the example was in the guide: class KaraokeSong < Song  
  # Format ourselves as a string by appending  
  # our lyrics to our parent's #to_s value.  
  def to_s  
    super + " [#{@lyrics}]"  
  end  
end  
aSong = KaraokeSong.new("My Way", "Sinatra", 225, "And now, the...")  
aSong.to_s

and here is an excerpt of test1.rb; my version of it to test out the example given in the guide: class Piece                                   # Define the Piece class.
    def initialize (name, composer, duration)  # Define initialize method, which is what parameters will get passed to
      @piecename = name                          # when there is an instance of the class.
      @composername = composer
      @piecelength = duration
    end
  end
  dvorakbmin = Piece.new("Cello Concerto in B minor [Finale]", "Dvorak", "13:00")
  print "instance dvorakbin being printed out: "
  puts dvorakbmin
  print "Call to to_s method of dvorakbmin instance: "
  dvorakbmin.to_s
  print "\n"
  class Piece                                   # ^ Extend the existing Piece class. ^
    def to_s                                    # Define the to_s method to print out the parameters.
      print "Your piece: #{@composername}'s #{@piecename} lasts #{@piecelength}."
    end
  end
  puts "This is the instance of the class Piece: "
  elgaremin = Piece.new("Cello Concerto in E minor mvt I", "Edward Elgar", "7:22")
  puts "This is the instance being put out."
  puts elgaremin
  puts "This is the call to the to_s method in Piece."
  elgaremin.to_s
  print "\n\n"
  print "Now defined will be the subclass 'MusicMinusOne' of the superclass 'Piece'.\n"
  class MusicMinusOne < Piece
    def initialize (name, composer, duration, instrument)
      super(name, composer, duration)
      @minusone = instrument
    end
  end
  print "This should display the name, composer, and duration of the MMO Dvorak, but without the subclass's @minusone instance variable: \n"
  dvorakMMO = MusicMinusOne.new("'Cello Concerto in B minor [Finale]", "Dvorak", "13:00", "violoncello")
  dvorakMMO.to_s
  class MusicMinusOne < Piece
    def to_s
      print super + "  The #{@minusone} instrumentalist will be accompanied by an orchestra."
    end
  end
  puts "\nNow displayed will be the newest version of our to_s method: "
  dvorakMMO.to_s
  print "\n"  The script executed as expected up until the very last three lines, where the newest version of the to_s method is output (dvorakMMO.to_s).  It gave me this error from the command line: Your Piece: Dvorak's 'Cello Concerto in B minor [Finale] lasts 13:00.C:/Documents and Settings/Josh Hanson/Desktop/Programming/Ruby/test1.rb:91:in 'to_s': undefined method '+' for nil:NilClass (NoMethodError) from C:/Documents and Settings/Josh Hanson/Desktop/Programming/Ruby/test1.rb:95

As far as I know the '+' is just the thing you use to concatenate (or whatever that word is that means to put two things together).  So why would it return that error?

-----------------------------------
wtd
Sat Aug 28, 2004 12:10 am


-----------------------------------
Piece#to_s (which is how I represent the to_s method of class Piece) is actually calling print.  The problem with this is that print returns nil.

"to_s" methods should always return a string.  As long as a class has a to_s method, you can call that method and print it as easily as:

puts my_instance

Which is equivalent to:

print my_instance.to_s
print "\n"

So your code cleaned up looks like:

class Piece                                  
	def initialize (name, composer, duration)  
		@piecename = name                         
		@composername = composer
		@piecelength = duration
	end
end

dvorakbmin = Piece.new("Cello Concerto in B minor [Finale]", "Dvorak", "13:00")

print "instance dvorakbin being printed out: "
puts dvorakbmin

print "Call to to_s method of dvorakbmin instance: "
puts dvorakbmin

class Piece                                
	def to_s                                    
		"Your piece: #{@composername}'s #{@piecename} lasts #{@piecelength}."
	end
end

elgaremin = Piece.new("Cello Concerto in E minor mvt I", "Edward Elgar", "7:22")

puts "This is the instance of the class Piece: "
puts "This is the instance being put out."
puts elgaremin

puts "This is the call to the to_s method in Piece."
puts elgaremin
puts

puts "Now defined will be the subclass 'MusicMinusOne' of the superclass 'Piece'."

class MusicMinusOne < Piece
	def initialize (name, composer, duration, instrument)
		super(name, composer, duration)
		@minusone = instrument
	end
end

puts "This should display the name, composer, and duration of the MMO Dvorak, but without the subclass's @minusone instance variable:"

dvorakMMO = MusicMinusOne.new("'Cello Concerto in B minor [Finale]", "Dvorak", "13:00", "violoncello")
puts dvorakMMO

class MusicMinusOne
	def to_s
		super + "  The #{@minusone} instrumentalist will be accompanied by an orchestra."
	end
end

puts "Now displayed will be the newest version of our to_s method: "
puts dvorakMMO

Note also that when you reopen an existing class to extend it, you don't need to show what it's inheriting from, as demonstrated where I've cleaned up your extension of the MusicMinusOne class.

You've got a good start, though.  Keep going!  :)

-----------------------------------
JHanson90
Sat Aug 28, 2004 12:41 am


-----------------------------------
Thanks for the help; it worked! :D

-----------------------------------
wtd
Sat Aug 28, 2004 3:49 am


-----------------------------------
You're quite welcome.  It odd, but very very nice to see Ruby code posted here that wasn't written by me. :)

-----------------------------------
JHanson90
Mon Aug 30, 2004 3:03 pm


-----------------------------------
Any Ruby IDE recommendations?  Right now I'm using FreeRIDE [Free Ruby IDE], but wondering if there's anything better that you recommend.  FreeRIDE was the default program that I got with Ruby, but it doesn't have as much syntax highlighting support as I would like.  Auto-indenting is nice too, but I don't necessarily need that.

-----------------------------------
wtd
Mon Aug 30, 2004 9:23 pm


-----------------------------------
I'm pretty happy just using Textpad (on Windows) and just having a cmd.exe window open.  On *nix systems I usually use either vim or a graphical text editor and have a terminal window open.

You might also try [url=http://www.scriptolutions.com/arachno_ruby.php]ArachnoRuby.  I know the guy who created it.  He's a brillant Ruby and Eiffel coder.

-----------------------------------
JHanson90
Mon Aug 30, 2004 9:38 pm


-----------------------------------
Cool, thanks again.

-----------------------------------
JHanson90
Tue Sep 14, 2004 7:23 pm


-----------------------------------
I want to test out the idea of using Ruby for the Web.  I noticed that Ruby's website, http://www.ruby-lang.org/, has a Ruby program as it's index.*.  http://www.ruby-lang.org/en/index.rb
So I found out about eRuby and mod_ruby.  Is eRuby any good? I'm some issues trying to install it; should I just ditch the idea?

eRuby claims to be just like JSP, ASP, or PHP, but with the power of Ruby, so that sorta made me wonder if this was a good idea...

-----------------------------------
wtd
Mon Sep 20, 2004 11:09 am


-----------------------------------
eRuby is incredibly cool, and you can use it as a program even without dealing with it via a web server.  It allows you to put Ruby code into a text file.

There's also an implementation of eRuby written in Ruby.  Try running the "erb" command.
