Project: Ruby in .NET

Filed under: Code Comments: 0

Here you will find the next in a series of projects where I had to write a simple application that demonstrated the ability of the .NET Framework and Visual Studio to be extended for languages other than those that come with the default installation.

This application demonstrates Ruby in .NET

rubyfile.rb

   1:  class Employee
   2:      #    class constructor
   3:      def initialize( ) 
   4:          @name = ""
   5:          @rate = 0
   6:          @hours = 0
   7:      end
   8:      
   9:      #    class constructor, first overload. Use to set @name when instantiating a new Employee object
  10:      def initialize( aName ) 
  11:          @name = aName
  12:          @rate = 0
  13:          @hours = 0
  14:      end
  15:      
  16:      #    class constructor, first overload. Use to set @name, @rate, & @hours when instantiating a new Employee object
  17:      def initialize( aName, aRate, aHours ) 
  18:          @name = aName
  19:          @rate = Float(aRate)
  20:          @hours = Float(aHours)
  21:      end
  22:      
  23:      #    Get() and Set() methods for @name variable
  24:      def setName( aName )
  25:          @name = aName
  26:      end
  27:      
  28:      def getName()
  29:          return @name
  30:      end
  31:      
  32:      #    Get() and Set() methods for @rate variable
  33:      def setRate( aRate )
  34:          @rate = Float(aRate)
  35:      end
  36:      
  37:      def getRate()
  38:          return @rate
  39:      end
  40:      
  41:      #    Get() and Set() methods for @hours variable
  42:      def setHours( aHours )
  43:          @hours = Float(aHours)
  44:      end
  45:      
  46:      def getHours()
  47:          return @hours
  48:      end
  49:      
  50:      #    Determine if employee earned overtime pay
  51:      def getOverTime()
  52:          if( @hours > 40 )
  53:              return (@hours - 40)
  54:          end
  55:          return 0
  56:      end
  57:      
  58:      #    Calculate employee's gross pay
  59:      def getGrossPay()
  60:          if( getOverTime() > 0 )
  61:              return ( (@hours*@rate) + (@rate*getOverTime())*1.5 )
  62:          else
  63:              return ( @hours*@rate )
  64:          end
  65:      end
  66:  end
  67:   
  68:  #    Custom method to determine if a string value can be parsed as a numeric value
  69:  def isNumeric(s)
  70:      begin
  71:          Float(s)
  72:      rescue
  73:          false # not numeric
  74:      else
  75:          true # numeric
  76:      end
  77:  end
  78:   
  79:  #
  80:  #    'main()'
  81:  #
  82:   
  83:  #    get user's name
  84:  puts( "Hello! And your name is...? " )
  85:  name = gets()
  86:   
  87:  #    get user's payrate
  88:  puts( "What is your payrate? " )
  89:  rate = gets()
  90:   
  91:  #    cycle error message until a valid payrate value is entered
  92:  while( !isNumeric(rate) )
  93:      puts( "Invalid payrate value! Please enter only numbers or a decimal point!" )
  94:      puts( "What is your payrate? " )
  95:      rate = gets()
  96:  end
  97:   
  98:  #    get user's work hours
  99:  puts( "How many hours did you work? " )
 100:  hours = gets()
 101:   
 102:  #    cycle error message until a valid hours value is entered
 103:  while( !isNumeric(hours) )
 104:      puts( "Invalid hours value! Please enter only numbers or a decimal point!" )
 105:      puts( "How many hours did you work? " )
 106:      hours = gets()
 107:  end
 108:   
 109:  #    create a new Employee object using name, rate, & hours
 110:  emp = Employee.new( name, rate, hours )
 111:   
 112:  #    display message if employee earned overtime
 113:  if( emp.getOverTime > 0 )
 114:      puts( "You earned overtime!" )
 115:  end
 116:   
 117:  #    print gross pay
 118:  puts( "Your gross pay will be $#{emp.getGrossPay()}" )
 119:   
 120:  #
 121:  #    End 'main()'
 122:  #

That's all. I didn't try writing a WinForm app with Ruby, simply because I didn't have a lot of time or inclination to do so when this project was due. What you see here is really just a simple Ruby app. The main point of the project was that Visual Studio was perfectly capable of being used as a Ruby IDE.

Source Code

As usual, the source code for this post can be downloaded: SENG.Ruby.App.zip