Last active
August 29, 2015 14:01
-
-
Save rubyonrailstutor/176579deb58cd7bf3b58 to your computer and use it in GitHub Desktop.
Example of a good gist
This file contains hidden or 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
# a good gist is a very few, small pieces of easily digestible code | |
# please break long, heavily commented files into smaller chunks, with clearly defined questions | |
# and all the context required to communicate to a user what is happening | |
def longest_string(list) | |
longest_so_far = list.first | |
for item in list | |
if item > longest_so_far | |
longest_so_far = item | |
end | |
end | |
return longest_so_far | |
end | |
# the method above doesn't really work because | |
longest_string(["aaaa", "ccccc", "zz"]) # could be expected to return "ccccc" but returns "zz" | |
# so think about what detail is actually being compared on line 8, ie the unicode value of the character or the length of the element ? |
Line #8: It's comparing the length of element. For instance, we want to see if "bobby" if is longer (character-wise) than "Johnny". If it is, we want "Bobby" to replace "Johnny".
If I invoke "ABC".length then it prints 3 to the value. If I do the "ABC".Length then that gives me an invalid method error.
Am I misunderstanding what the point of the longest_string & shortest_string is truly for?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If it's returning 'ZZ', then that means it isn't measuring the length of the element but rather the unicode value.