-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmethods_chris_pine.rb
69 lines (53 loc) · 1.99 KB
/
methods_chris_pine.rb
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#methods
#methods are things that do stuff! if objects (like strings, integers, floats) are nouns in Ruby language, then methods are like verbs. Like in English you can't have a noun without a verb.
#As every verb needs a noun, so every method needs an object
#Methods seen so far!
#puts
#gets
#chomp
#.to_s
#.to_i
#.to_f
#/
#*
#+
#-
#self
#It's a special variable which points to whatever object you are in.
#every method is being done by some object, even if it doesn't have a dot in front of it/
#Fancy string methods
#reverse
#var1 = "stop"
#puts var1.reverse # => pots
#doesn't reverse original string though -makes a new backwards version of it
#length
#number of characters including spaces in the string
puts "What is your full name?"
name = gets.chomp
puts "Did you know there are " +name.length.to_s + " characters in your name, " + name + "?"
puts "What is your first name?"
first_name = gets.chomp
puts "What is your second name?"
second_name = gets.chomp
puts "What is your last name?"
last_name = gets.chomp
full_name = first_name.to_s + second_name.to_s + last_name.to_s
puts "Did you know there are " + full_name.length.to_s + " characters in your name, " + first_name + " " + second_name + " " + last_name + " ?"
#throughout these method calls letters remains unchanged, some methods do change associated objects (but not these)
letters = "aAbBcCdDeE"
puts letters.upcase #AABBCCDDEE
puts letters.downcase #aabbccddee
puts letters.swapcase #AaBbCcDdEe
puts letters.capitalize #Aabbccddee
puts " a".capitalize #" a" -capitalise method only capitalises 1st index
puts letters #aAbBcCdDeE
#visual formatting
#can centre strings by adding linewidth & center method
lineWidth = 50 #stored so the width can easily be changed
puts( "Old Mother Hubbard".center(lineWidth))
#ljust -left justify rjust -right justify - similar to center except they pad the string with spaces on the right & left hand sides
lineWidth = 4-
str = "text"
puts str.ljust lineWidth
puts str.center lineWidth
puts str.rjust lineWidth