-
-
Save Phrogz/1211994 to your computer and use it in GitHub Desktop.
example
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
# Define a method that takes a path to a file and returns an array of all words therein | |
def read_words( filename ) | |
# Read in the contents of the file and split it into | |
# an array of strings by finding (and discarding) any "whitespace" | |
# (spaces, tabs, newlines) | |
File.read(filename).split(/\s+/) | |
end | |
# Define a method that takes an array of words and returns an array without any 'junk' words | |
def remove_useless_words( all_words ) | |
useless_words = ["word1", "word2"] | |
all_words - useless_words # Last expression in the method is what it returns | |
end | |
# Call the 'read_words' method, passing in a string as the argument | |
# Save the resulting array of words in a variable named "words" | |
words = read_words "filename.txt" | |
# Now, call the 'remove_useless_words' method, passing that array in | |
# Save the resulting array in a variable named "good_words" | |
good_words = remove_useless_words( words ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment