Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Some Ruby Help
Index -> Programming, Ruby -> Ruby Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
JHanson90




PostPosted: Fri Aug 27, 2004 11:37 pm   Post subject: 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:
code:
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:
code:
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:
code:
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?
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Sat Aug 28, 2004 12:10 am   Post subject: (No subject)

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:

code:
puts my_instance


Which is equivalent to:

code:
print my_instance.to_s
print "\n"


So your code cleaned up looks like:

code:
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! Smile
JHanson90




PostPosted: Sat Aug 28, 2004 12:41 am   Post subject: (No subject)

Thanks for the help; it worked! Very Happy
wtd




PostPosted: Sat Aug 28, 2004 3:49 am   Post subject: (No subject)

You're quite welcome. It odd, but very very nice to see Ruby code posted here that wasn't written by me. Smile
JHanson90




PostPosted: Mon Aug 30, 2004 3:03 pm   Post subject: (No subject)

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




PostPosted: Mon Aug 30, 2004 9:23 pm   Post subject: (No subject)

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 ArachnoRuby. I know the guy who created it. He's a brillant Ruby and Eiffel coder.
JHanson90




PostPosted: Mon Aug 30, 2004 9:38 pm   Post subject: (No subject)

Cool, thanks again.
JHanson90




PostPosted: Tue Sep 14, 2004 7:23 pm   Post subject: (No subject)

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...
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Mon Sep 20, 2004 11:09 am   Post subject: (No subject)

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.
Display posts from previous:   
   Index -> Programming, Ruby -> Ruby Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: