Guest User

Untitled

a guest
Jan 22nd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. # Define a method that takes a path to a file and returns an array of all words therein
  2. def read_words( filename )
  3. # Read in the contents of the file and split it into
  4. # an array of strings by finding (and discarding) any "whitespace"
  5. # (spaces, tabs, newlines)
  6. File.read(filename).split(/\s+/)
  7. end
  8.  
  9. # Define a method that takes an array of words and returns an array without any 'junk' words
  10. def remove_useless_words( all_words )
  11. useless_words = ["word1", "word2"]
  12. all_words - useless_words # Last expression in the method is what it returns
  13. end
  14.  
  15. # Call the 'read_words' method, passing in a string as the argument
  16. # Save the resulting array of words in a variable named "words"
  17. words = read_words "filename.txt"
  18.  
  19. # Now, call the 'remove_useless_words' method, passing that array in
  20. # Save the resulting array in a variable named "good_words"
  21. good_words = remove_useless_words( words )
Add Comment
Please, Sign In to add comment