-
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
6 changed files
with
195 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,30 @@ | ||
# Operator overloading in Ruby | ||
|
||
class Animal | ||
|
||
attr_accessor:name, :trait | ||
|
||
def initialize(name, trait) | ||
|
||
@name = name | ||
@trait = trait | ||
|
||
end | ||
|
||
def +(other_object) | ||
|
||
return Animal.new("#{self.name}#{other_object.name}", "#{self.trait}#{other_object.trait}") | ||
|
||
end | ||
|
||
end | ||
|
||
class Zebra < Animal | ||
|
||
end | ||
|
||
a = Zebra.new("Filly", "Brave") | ||
|
||
b = Zebra.new("Mega", "Smart") | ||
|
||
puts (a + b) .inspect |
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,14 @@ | ||
# Operator overloading in Ruby | ||
# <", ">", "=" Comparable operators | ||
|
||
include Comparable | ||
|
||
def <=> (other) | ||
|
||
return self.name<=>other.name | ||
|
||
end | ||
|
||
puts "Filly"<=>"Filly" | ||
|
||
puts "Filly"<=>"Mega" |
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,18 @@ | ||
# Operator overloading in Ruby | ||
# <", ">", "=" Comparable operators | ||
|
||
include Comparable | ||
|
||
def <=> (other) | ||
|
||
return self.value<=>other.value | ||
|
||
end | ||
|
||
puts "Filly"<=>"Filly" | ||
|
||
puts "Filly"<=>"Mega" | ||
|
||
puts 10<=>20 | ||
|
||
puts 100<=>20 |
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,32 @@ | ||
# Operator overloading in Ruby | ||
# <", ">", "=" Comparable operators | ||
|
||
class Myclass | ||
|
||
include Comparable | ||
attr_accessor:myname | ||
|
||
def initialize(name) | ||
|
||
@myname = name | ||
|
||
end | ||
|
||
def <=> (other) | ||
|
||
return self.myname<=>other.myname | ||
|
||
end | ||
|
||
end | ||
|
||
obj1 = Myclass.new("Filly") | ||
obj2 = Myclass.new("Mega") | ||
|
||
puts obj1 > obj2 | ||
|
||
puts obj1 < obj2 | ||
|
||
puts obj1 == obj2 | ||
|
||
puts obj1 != obj2 |
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,57 @@ | ||
# Operator overloading in Ruby | ||
# +", "-", "*", "/", "*", "%", "**" Operators | ||
|
||
class Tester | ||
|
||
attr_accessor:num | ||
|
||
def initialize(num) | ||
|
||
@num = num | ||
|
||
end | ||
|
||
def +(other) | ||
|
||
return self.num+other.num | ||
|
||
end | ||
|
||
def *(other) | ||
|
||
return self.num*other.num | ||
|
||
end | ||
|
||
def /(other) | ||
|
||
return self.num/other.num | ||
|
||
end | ||
|
||
def %(other) | ||
|
||
return self.num%other.num | ||
|
||
end | ||
|
||
def **(other) | ||
|
||
return self.num**other.num | ||
|
||
end | ||
|
||
end | ||
|
||
a = Tester.new(5) | ||
b = Tester.new(3) | ||
|
||
puts a+b | ||
|
||
puts a*b | ||
|
||
puts a/b | ||
|
||
puts a%b | ||
|
||
puts a**b |
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,44 @@ | ||
# Operator overloading in Ruby | ||
# "[]", "[]", "<<", Operators using Array | ||
|
||
class Tester | ||
|
||
attr_accessor:arr | ||
|
||
def initialize(*arr) | ||
|
||
@arr = arr | ||
|
||
end | ||
|
||
def [] (x) | ||
|
||
return @arr[x] | ||
|
||
end | ||
|
||
def []=(x, value) | ||
|
||
@arr[x] = value | ||
|
||
end | ||
|
||
def <<(x) | ||
|
||
@arr << x | ||
|
||
end | ||
|
||
end | ||
|
||
a = Tester.new(0,1,2,3) | ||
|
||
puts a[3] | ||
|
||
a << 97 | ||
|
||
puts a[4] | ||
|
||
a[5] = 101 | ||
|
||
puts a[5] |