Saturday, March 29, 2014

Ruby Substring Tutorial - Finding if a substring is in an array with the Include? method

Finding Substrings in Ruby

In data harvesting (web scraping) applications  a primary goal is often to extract links from the downloaded file. In order to accomplish this, the downloaded file can be searched for the "href" tag to find if there are indeed links in the file.

Often the individual lines in the downloaded files are first placed in an array and then the array is searched for entries that contain the href tag. Searching an array for a substring can be accomplished with the include? method. However this method can not be applied to the entire array at once. It must be applied to the individual elements of the array one at a time

The code below illustrates this point. It also illustrates that the include? method can be used with either single or double quotes as a parameter. And it illustrates that the parenthesis are optional for the substring parameter that is being searched for.

To test the code, copy and paste the lines below in a text editor. If you are using Notepad, do not save the file as a text file, but save it with the "all files" selection with an rb extension.

#lesson_finding_substrings.rb

a = 'is the substring in here'
puts a.include? 'ing'
puts a.include? "ere"

b = "<a>"
c = "<a>"


puts b.include? 'a'      # use of single quotes returns true
puts c.include? ">"     #use of double quotes and no parenthesis. Returns true
puts c.include? (">")    # the equivalent command with parenthesis. Returns true

d=[]

d[0] = 'href="http://santa-rosa-algebra-geometry-statistics--tutoring.com/3D-Math-graphic-design-santa-rosa-ca.html"'

#the above illustrates use of single quotes.Note that an escape character is not needed to for the insides quotes of the URL

puts d.include? 'href'#returns false

puts d[0].include? 'href' #returns true

#Run Program
#C:\Ruby193>ruby lesson_finding_substrings.rb


#Code Output

#true
#true
#true
#true
#true
#false
#true



No comments:

Post a Comment