Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The following statement assigns empty string to <code>search</code> variable if <code>search</code> is not defined. </p> <pre><code>search = "" unless search </code></pre> <p>Had this assignment not be done, <code>Regexp.new</code> would have thrown an <code>TypeError</code> with message <code>no implicit conversion of nil into String</code>, or if search was not defined then <code>NameError</code> with message undefined local variable or method 'search'...</p> <p>In the following statement: </p> <pre><code>matched_games = games.grep(Regexp.new(search)) </code></pre> <p><code>games.grep(pattern)</code> returns an array of every element that matches the pattern. For further details please refer to <a href="http://ruby-doc.org/core-2.0/Enumerable.html#method-i-grep" rel="nofollow">grep</a>. <code>Regexp.new(search)</code> constructs a new regular expression from the supplied <code>search</code> variable which can either be a string or a regular expression pattern. Again, for further details please reference <a href="http://www.ruby-doc.org/core-2.0/Regexp.html#method-c-new" rel="nofollow">Regexp::new</a></p> <p>So say for example search is <code>""</code> (empty string), then <code>Regexp.new(search)</code> returns <code>//</code>, if search = 'Super Mario Bros.' then <code>Regexp.new(search)</code> returns <code>/Super Mario Bros./</code>.</p> <p>Now the pattern matching: </p> <pre><code># For search = "", or Regexp.new(search) = // matched_games = games.grep(Regexp.new(search)) Result: matched_games = ["Super Mario Bros.", "Contra", "Metroid", "Mega Man 2"] # For search = "Super Mario Bros.", or Regexp.new(search) = /Super Mario Bros./ matched_games = games.grep(Regexp.new(search)) Result: matched_games = ["Super Mario Bros."] # For search = "something", or Regexp.new(search) = /something/ matched_games = games.grep(Regexp.new(search)) Result: matched_games = [] </code></pre> <p>Hope this makes sense.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload