-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint.rb
More file actions
46 lines (36 loc) · 789 Bytes
/
print.rb
File metadata and controls
46 lines (36 loc) · 789 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Prinintg methods in ruby
class PrintCases
def initialize(name, age)
@name = name
@age = age
end
def printP
p self
end
def printPuts
puts self
end
def printPrint
print self
end
end
obj1 = PrintCases.new("Anshika", 20)
# puts is human-friendly, converts argument to string(to_s) and adds new line
obj1.printPuts
# p is developer friendly, prints raw, insepectable form (using inspect)
obj1.printP
# print and puts call to_s method by default on objects, if we wish to change the way they look, we may override to_s
obj1.printPrint
class PrintModified
def initialize(name, age)
@name = name
@age = age
end
def to_s
"Name: #{@name}, Age: #{@age} \n"
end
end
obj2 = PrintModified.new("Anshika", 20)
print obj2
puts obj2
p obj2