jruby – Sample App – Classes – blackBook

Background

Now that we have jruby installation out of the way let us create a simple app that creates a class.

BTW, our jruby Installation post is here.

 

Code

blackbook.rb


###############################################
#Foundational Sample Script
#https://gist.github.com/dougal/24876
###############################################
require 'date'

class Blackbook

    #Declare Accessor Methods   
    attr_accessor :name, :relationship, :dateAdded

    
    # ruby class variables are prefixed with '@@' .
    # set number of entries
    @@iNumberofEntries = 0;
    
    #Constructor
    def initialize(name, relationship, dateAdded)
    
        @name = name
        @relationship = relationship
        @dateAdded = dateAdded
        
        #Increment number of entries
        @@iNumberofEntries = @@iNumberofEntries + 1
        
    end
  
    # Class Method numberofEntries
    # Returns Class Variable @@iNumberofEntries
    def self.numberofEntries
    
        return @@iNumberofEntries
        
    end  
    
end

# Get Current Date
dtNow = Time.now

# Date Formats
dateFormat="%b %d %Y"
dateFormatYYYYMMDD="YYYYMMDD"

# Create the new instance ( Prince Charles)
dob = "19481114"
dobCharles = Date.parse(dob);
entryCharles = Blackbook.new('Charles', 'Prince', dobCharles)

# Access it's attributes.
puts "Entry Name is " + entryCharles.name
puts "Relationship is " + entryCharles.relationship
puts "Date Added is " + entryCharles.dateAdded.strftime(dateFormat);
puts ""

# Create the new instance ( Lady Diana )
dob = "19610701"
dobDiana = Date.parse(dob);
entryDiana = Blackbook.new('Diana', 'Princess', dobDiana)

# Access it's attributes.
puts "Entry Name is " + entryDiana.name
puts "Relationship is " + entryDiana.relationship
puts "Date Added is " + entryDiana.dateAdded.strftime(dateFormat);
puts ""

#Get Number of entries
strNumberofEntries = Blackbook.numberofEntries.to_s
puts "Number of entries in black book " + strNumberofEntries

invoke.cmd


setlocal

REM SET JAVA_HOME to Version 1.8
set "JAVA_HOME=C:\Program Files\Java\jdk1.8.0_181"
rem set "JAVA_HOME=C:\Program Files\Java\jdk-9.0.4"
REM set "JAVA_HOME=C:\Program Files\Java\jdk-10.0.2"

set "JRUBY_BIN=C:\Downloads\jruby\v9.2\zip\jruby-dist-9.2.0.0-bin\jruby-9.2.0.0\bin"

set "_app=blackbook.rb"

%JRUBY_BIN%\jruby.bat "%_app%"

endlocal

output

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s