Ruby Language

Syntax

Classes

class Tale
end

Attribute

class Tale
@author
@genre
@tale_body
end

Methods

class Tale
def initialize(author, genre, body)
@author=author
@genre=genre
@tale_body=body
end
#method to display part of the tale
def tell part_to_be_told
start_index=@tale_body.index(part_to_be_told)
print @tale_body\
[start_index+part_to_be_told.length,@tale_body.length]
end
#getter for author attribute
def author
@author
end
#setter for author attribute
def author=(newAuthor)
@author=newAuthor
end
end

Objects

mermaid_tale=Tale.new("unknown","fairy_tale", "once upon a time….")

Inheritance

class FantasyTale < Tale
def initialize (author, genre, body, creature)
super (author, genre, body)
@creature = creature
end
end

Module

module Tales
class Tale
end
class FantasyTale < Tale
end
end
 
T1 = Tales :: Tale.new("Jim Henson", "Fantasy", "Once upon a time….")

Data Types

#number
tale_age=1000
or
tale_age=1_000
#float
diameter=3.145
or
diameter=3.14e-05
#string
s= "It's a beautiful life"
or
s1= 'It's a beautiful life'
#label
:part_of_story
or
:next_part

Blocks and Iterators

#Block
5.times{|msg|
print msg
}
#iterator
<collection>.each {|<var>| <operation on var>}
 
alist=[1,2,3.4,"foo"]
alist.each {|item| puts item}
 
>ruby iterator_example.rb
1
2
3.4
foo
>Exit code: 0
 
#example
class Tale
#constructor
def initialize(author, genre, body)
@author=author
@genre=genre
@tale_body=body
end
#method to display part of the tale
def tell part_to_be_told
start_index=@tale_body.index(part_to_be_told)
print @tale_body\
[start_index+part_to_be_told.length,@tale_body.length]
end
#getter for author attribute
def author
@author
end
#setter for author attribute
def author=(newAuthor)
@author=newAuthor
end
def write_to_file
#opens a file and does the writing within it
File.open ('tale.txt','w') { |file|
file.puts @author
file.puts @genre
file.puts @tale_body
}
end
end
 
#example2
class Tale
#constructor
def initialize(author, genre, body)
@author=author
@genre=genre
@tale_body=body
end
#method to display part of the tale
def tell part_to_be_told
start_index=@tale_body.index(part_to_be_told)
print @tale_body\
[start_index+part_to_be_told.length,@tale_body.length]
end
#getter for author attribute
def author
@author
end
#setter for author attribute
def author=(newAuthor)
@author=newAuthor
end
#method to write the attribute values to a file
def write_to_file
#opens a file and does the writing within it
File.open ('tale.txt','w') { |file|
file.puts @author
file.puts @genre
file.puts @tale_body
}
end
#method to read from a file
def read_from_file
File.readlines('tale.txt').each { |line|
puts line
}
end
end

Exception Handling

def write_to_file
#opens a file and does the writing within it
begin
File.open ('tale.txt','w') { |file|
file.puts @author
file.puts @genre
file.puts @tale_body
}
rescue Exception => ex
$stderr.print  "File open failed  "
end
end

Data Structures

#array
books = ["The Treasure Island", "Don Quixote"]
 
books[0]
 
#hash
book = {"title" => "The Treasure Island"
, "author" => "R.L.Stevenson"
"genre" => "Adventure"
}
 
book["title"]