- .upcase and .downcase
So methods are like commands except for the following (that I know of):
- A period is used before a method
- You can have more than one method on a line and Ruby will follow the sequence from left to right
Remember in Ruby everything is case sensitive and just like with commands capitilizing any letter in a method will cause an error.
So to explain what the 4 methods in this section does.
.length returns the number of characters in string including numbers, letters, spaces, and symbols.
- Input: "David" .length
- Input: "David ".length
Note: Like setting variables the spaces between the method and string from the period does not affect the output. For instance ( "David" . length ) OR ("David". length) will both give us an output of 5.
.reverse returns the string backwards.
- Input: "David" .reverse
. upcase capitalizes every letter in the string and .downcase lowercases every letter in the string.
- Input: "David" .upcase
- Input: "David" .downcase
Remember when I said methods can be on the same line as a command? And that Ruby will do them in order of left to right? Well I did some testing on matter.
- Input: print "david" .upcase
Lesson: Command and methods can exist on the same line.
- Input: print "david" .upcase.downcase
Lesson: Ruby follows commands/methods from left to right
- Input: print "david" .upcase.downcase.length
Lesson: Same as test 2
- Input: print "david" .length.upcase.downcase
Ouput: error: undefined method 'upcase' for 5
Lesson: Same as test 2 and that the .upcase and .downcase methods will cause error for numbers
- Input: "david5" .upcase
Lesson: Tested for .downcase and with symbols. .downcase and .upcase will work as long as there is a letter in the string.
- Input: "david".upcase print "hi"
Lesson: Commands cannot be after methods
- Input: print "david".upcase
Input: puts "david".upcase
Lesson: Commands do not get overwritten by methods following it. This is an important one since all the other tests could have been concluded that the command was overwritten.