Skip to content

Instantly share code, notes, and snippets.

@sameera207
Last active August 29, 2015 14:05
Show Gist options
  • Save sameera207/c0b9f89fa326bb23ff23 to your computer and use it in GitHub Desktop.
Save sameera207/c0b9f89fa326bb23ff23 to your computer and use it in GitHub Desktop.
Explanation for Regexp.new(search)
#Following is the problem with your solution. You have 2 if's , in ruby we need only 1 if
# notice that it has only one IF :)
if matched_games.length > 0 && matched_games.include?(search)
puts "Game #{search} found."
end
#clarrifications
Regexp.new(search) #is creating a new regular expression object in ruby.
#<Any Object>.new is the way of initializing an object in ruby. In this case 'Regexp' class.
#Regular expression can me used to match words in different options. Like
# Ex: We have two string
# 1 - Sam
# 2 - Sameera
# regexp = Regexp.new("Sam")
# regexp.match("Sameera")
# read more about ruby regex here (http://ruby-doc.org/core-1.9.2/Regexp.html)
games.grep #will an array of every element in enum for which match the pattern. This is a method of 'Enumerable', and 'Array' is
#uses 'Enumerable' class.
# read more here (http://ruby-doc.org/core-2.1.2/Enumerable.html#method-i-grep)
#So what is happening here is...
games.grep(Regexp.new(search))
# Regexp.new(search) - creating a new RegularExpression with the search word 'Super Mario Bros.'
# then it passes to 'games.grep' array, 'games' is an Array.
# then it returns the matching words
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment