Skip to content

Commit

Permalink
Ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
msatmod committed Aug 30, 2022
1 parent 2361b03 commit 6228441
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Day 4/Getter_&Setter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Program of Getter, Setter and Initialize Method in Ruby Class

class Rectangle
# Constructor
def initialize(l,b)
@length = l
@breadth = b
end
# Getter
def getLength
return @length
end

def getBreadth
return @breadth
end
end

# Creating an object
rect = Rectangle.new(50,30)

# Getter
x = rect.getLength
y = rect.getBreadth

puts "The length of rectangle is: #{x}"
puts "The breadth of rectangle is: #{y}"
45 changes: 45 additions & 0 deletions Day 4/Getter_Setter_&_Initialize_Method.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Program of Getter, Setter and Initialize Method in Ruby Class

class Rectangle
# Constructor
def initialize(l,b)
@length = l
@breadth = b
end
# Setter
def setLength=(value)
@length = value
end
def setBreadth=(value)
@breadth = value
end
# Getter
def getLength
return @length
end

def getBreadth
return @breadth
end
# Area of rectangle
def calculateArea
return @length * @breadth
end

end

# Creating an object
rect = Rectangle.new(50,30)
# Using Setters
rect.setLength = 20
rect.setBreadth = 5

# Getter
x = rect.getLength
y = rect.getBreadth

area = rect.calculateArea

puts "The length of rectangle is: #{x}"
puts "The breadth of rectangle is: #{y}"
puts "The area is: #{area}"
22 changes: 22 additions & 0 deletions Day 4/Shortcut_of_getter_setter_and to_s_method.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Program for shortcut of getter, setter and also program of to_s method

class Animal
# Setter & Getter
attr_accessor :name, :age, :trait

def to_s
return "The pet name is #{name}, its age is #{age}, and it is #{trait}"
end
end

firstAnimal = Animal.new

firstAnimal.name = "Kitty"
firstAnimal.age = 12
firstAnimal.trait = "loudy"

puts firstAnimal.name
puts firstAnimal.age
puts firstAnimal.trait

puts firstAnimal.to_s
13 changes: 13 additions & 0 deletions Day 4/Shortcut_to_Getter_&_Setter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Shortcut to Getter & Setter

class Rectangle
attr_accessor :length, :breadth
end

rect = Rectangle.new

rect.length=50
rect.breadth=30

puts rect.length
puts rect.breadth
17 changes: 17 additions & 0 deletions Day 4/to_s_method.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# program for to_s method

class Rectangle
def initialize(l,b)
@l = 50
@b = 30
end

def to_s
return "Length is #{@l} & breadth is #{@b}"
end
end

rect = Rectangle.new(20,10)

puts "#{rect}"
puts rect

0 comments on commit 6228441

Please sign in to comment.