Skip to content

Instantly share code, notes, and snippets.

@Phrogz
Forked from AndrewVos/example.rb
Created September 12, 2011 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Phrogz/1211994 to your computer and use it in GitHub Desktop.
Save Phrogz/1211994 to your computer and use it in GitHub Desktop.
example
# 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