obj = ClassName.new
class Box
def initialize
# do something
end
end
#class with variable types
$global_var = "global"
class Greeter
attr_accessor :surname
def initialize(name = "World")
@name = name
@surname = "joshi"
@@company = "WGBL"
local_name = "local"
end
def access
puts "Access global variable #{$global_var}"
puts "Access class variable #{@@company}"
puts "Access instance variable #{@name}"
puts "Access instance variable #{local_name}"
end
end
#getter method
def width
@width
end
#setter method
def width=(w)
@width = w
end
class Box
# generate getter method for width
attr_reader :width
# generate setter method for width
attr_writer :width
# generate getter and setter methods for
#height
attr_accessor :height
end
class Box
def initialize(w,h)
@width, @height = w, h
@@count +=1
end
def self.count # class method
@@count
end
def area # instance method
@width * @height
end
end
class Animal
def make_noise
“awww……”
end
end
class Dog < Animal
def make_noise # overriding method
“bhow bhow”
end
end
class Cat < Animal
def walk
“walking”
end
end
module Utility
def self.sin(a)
# do something
end
class Language
def hindi_hello
puts “namaste”
end
end
end
module A
def a
end
end
module B
def b
end
end
class Sample
include A
include B
def s
end
end
f = File.new(“filename”, “mode”)
# process file
# closing the file after performing options is important
f.close
File.open(“filename”, “mode”) do |file|
# process files
end
Dir.pwd # gives current directory
Dir.chdir(“path”) # change current directory to specified path
Dir.entries(“path”) # gives list of entries in given path
Dir.mkdir “path” #creates directory specified in path