Docsity

Introduction to Ruby, Lecture notes of Computer Science

Toggle sidebar
bg1
Getting Started with Ruby
To install Ruby, visit https://www.ruby-lang.org/en/downloads/ and select the appropriate option from
the “Ways of Installing Ruby” list. For example, I clicked on the RubyInstaller for Windows machines,
clicked the big red Download button from the next page, and then selected the latest Ruby installer with
DevKit. Follow the instructions to install Ruby once your download finishes.
Now, open a code editor (I used Visual Studio) and create a blank text file called “test.rb”. Note that you
might need to make sure that your code editor did not place an extra “.txt” at the end of your file name
(It should just be called “test.rb” and not “test.rb.txt”. Copy the following code into this file:
#PROGRAMMER: Dan Cliburn
#DATE: 9/5/24
#FILE: test.rb
#This file provides a short example of how to code in Ruby
#This is the defintion of our Base class
class Base
#this is how we define a class method
def what
"Base"
end
end
#Derived1 inherits from Base
class Derived1 < Base
#what is an example of a polymorphic method
def what
"Derived1"
end
end
#Derived2 also inherits from Base
class Derived2 < Base
#what is an example of a polymorphic method
def what
"Derived2"
end
end
#The Composition class is composed of objects from Base, Derived1, and Derived2
class Composition
attr_accessor :arr; #this is how you create a class variable
(attribute) in Ruby
#arr is encapsulated inside of the Composition class
#initialize() is equivalent to a constuctor in C++
def initialize()
@arr = [ [Base.new, Derived1.new],
[Derived2.new, Base.new] ]
#NOTE: In methods you put the @ symbol in front of member variables
#This is how Ruby knows it is the member variable and not a local
variable
end
pf3

Partial preview of the text

Download Introduction to Ruby and more Lecture notes Computer Science in PDF only on Docsity!

Getting Started with Ruby

To install Ruby, visit https://www.ruby-lang.org/en/downloads/ and select the appropriate option from

the “Ways of Installing Ruby” list. For example, I clicked on the RubyInstaller for Windows machines,

clicked the big red Download button from the next page, and then selected the latest Ruby installer with

DevKit. Follow the instructions to install Ruby once your download finishes.

Now, open a code editor (I used Visual Studio) and create a blank text file called “test.rb”. Note that you

might need to make sure that your code editor did not place an extra “.txt” at the end of your file name

(It should just be called “test.rb” and not “test.rb.txt”. Copy the following code into this file:

#PROGRAMMER: Dan Cliburn #DATE: 9/5/ #FILE: test.rb #This file provides a short example of how to code in Ruby #This is the defintion of our Base class class Base #this is how we define a class method def what "Base" end end #Derived1 inherits from Base class Derived1 < Base #what is an example of a polymorphic method def what "Derived1" end end #Derived2 also inherits from Base class Derived2 < Base #what is an example of a polymorphic method def what "Derived2" end end #The Composition class is composed of objects from Base, Derived1, and Derived class Composition attr_accessor :arr; #this is how you create a class variable (attribute) in Ruby #arr is encapsulated inside of the Composition class #initialize() is equivalent to a constuctor in C++ def initialize() @arr = [ [Base.new, Derived1.new], [Derived2.new, Base.new] ] #NOTE: In methods you put the @ symbol in front of member variables #This is how Ruby knows it is the member variable and not a local variable end

#prints the contents of arr to the screen def printArray for r in 0..1 do for c in 0..1 do print "#{@arr[r][c].what} " end puts "" end end #prints the object in arr at row and col def objectAtIn2Darray(row, col) if row >= 0 and row < 2 and col >= 0 and col < 2 puts "Item at (#{row},#{col}) is #{@arr[row][col].what}" else puts "That row and column is out of bounds!" end end #prints a random object in arr to the screen def randomObjectIn2Darray row = rand(2); #rand(2) generates a random number from 0 to 1 col = rand(2); #rand(2) generates a random number from 0 to 1 puts "Item at (#{row},#{col}) is #{@arr[row][col].what}" end def doStuff choice = "5" while choice != "4" puts "What do you want to do:" puts " 1) Print object in the array at a specific row and column" puts " 2) Print a random object in the array" puts " 3) Print the entire array" puts " 4) Quit" print "ENTER CHOICE: " choice = gets.chomp #.chomp removes the newline from the input if choice == "1" print "What row do you want to see (enter 0 or 1): " row = gets.chomp.to_i #remove new line and convert to an integer print "What col do you want to see (enter 0 or 1): " col = gets.chomp.to_i #remove new line and convert to an integer objectAtIn2Darray(row, col) elsif choice == "2" randomObjectIn2Darray elsif choice == "3" printArray elsif choice == "4" puts "Bye!" else puts "Invalid choice!" end end end end