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
BookMark Magazine, an educational publication from BookMarkTutoring.com, provides educational supplemental material for students and teachers. These range from captivating educational classroom activities, such as click and color educational coloring pages and our Infinity Machine online drawing software (mathematical brush driven) to subject specific learning link libraries.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment