-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |