Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 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 )
Add Comment
Please, Sign In to add comment