Ruby Testing 2, 11/9/2004
What is Ruby? Scripting language, developed by Yukihiro “Matz” Matsumoto Current version = 1.8
Ruby Documentation Pickaxe book (shown last class) http://www.ruby-doc.org ri command ri <Class name> Ruby Cheat Sheet (on blackboard)
Running Ruby Command line Eclipse SciTE irb ruby –w <file name> More later SciTE Demo irb
Ruby in Eclipse Download Ruby Development Tool from http://rubyeclipse.sourceforge.net Nowhere near the functionality of Java in Eclipse In preferences, set path to ruby bin directory
Ruby Basics Everything is an object Comments start with # and go until end of line Methods are created using def…end
Ruby Naming Initial letter Multi-word names End characters Local variables, method parameters, and method names lowercase letter or underscore Global variable $ Instance variable @ Class variable @@ Class names, module names, constants uppercase letter Multi-word names Instance variables separate words with underscores Class names use MixedCase End characters ? Indicates method that returns true or false to a query ! Indicates method that modifies the object in place rather than returning a copy
Blocks Allow passing chunks of code in to methods Single line blocks enclosed in {} Multi-line blocks enclosed in do…end Receiving method uses “yield” command to call passed code (can call yield multiple times) Can use parameters [ 1, 3, 4, 7, 9 ].each {|i| puts i }
Strings in Ruby Quotes matter! Strings in “” (double quotes) have replacement processing on them \<char> replacement (like C -- \n = newline, etc) Expression replacement (“Variable x = #{x}” would have the value of x substituted for the #{x}) Strings in ‘’ (single quotes) are treated as literals with no processing Strings in `` (back ticks) are treated as commands to be executed by the operating system
Control structures If…elsif…else…end unless <condition> … end while <condition>… end until <condition>… end case when <condition> then <value>… else… end #.times (e.g. 5.times()) #.upto(#) (e.g. 3.upto(6)) <collection>.each {block}
Ruby equivalents in Java null toString() extends constructor name = class name try…catch import Ruby nil to_s < constructor name = initialize begin…end…rescue require
Test::Unit Very similar to JUnit require ‘test/unit’ Class MyTestClass < Test::Unit::TestCase Use “flunk” instead of JUnit’s “fail”