chuckfinley
chuckfinley

Reputation: 763

Already initialized constant warnings

I'm using Nokogiri code to extract text between HTML nodes, and getting these errors when I read in a list of files. I didn't get the errors using simple embedded HTML. I'd like to eliminate or suppress the warnings but don't know how. The warnings come at the end of each block:

extract.rb:18: warning: already initialized constant EXTRACT_RANGES
extract.rb:25: warning: already initialized constant DELIMITER_TAGS

Here is my code:

#!/usr/bin/env ruby -wKU
require 'rubygems'
require 'nokogiri'
require 'fileutils'

source = File.open('/documents.txt')
source.readlines.each do |line|
  line.strip!
  if File.exists? line
    file = File.open(line)

doc = Nokogiri::HTML(File.read(line))

# suggested by dan healy, stackoverflow 
# Specify the range between delimiter tags that you want to extract
# triple dot is used to exclude the end point
# 1...2 means 1 and not 2
EXTRACT_RANGES = [
  1...2
 ]

# Tags which count as delimiters, not to be extracted
DELIMITER_TAGS = [
  "h1",
  "h2",
  "h3"
]

extracted_text = []

i = 0
# Change /"html"/"body" to the correct path of the tag which contains this list
(doc/"html"/"body").children.each do |el|

  if (DELIMITER_TAGS.include? el.name)
    i += 1
  else
    extract = false
    EXTRACT_RANGES.each do |cur_range|
      if (cur_range.include? i)
        extract = true
        break
      end
    end

    if extract
      s = el.inner_text.strip
      unless s.empty?
        extracted_text << el.inner_text.strip
      end
    end
  end
end

print("\n")
puts line
print(",\n")
# Print out extracted text (each element's inner text is separated by newlines)
puts extracted_text.join("\n\n")
  end
end

Upvotes: 1

Views: 1838

Answers (3)

the Tin Man
the Tin Man

Reputation: 160551

As a programming tip:

Be careful using ... vs. .. for range definitions. The three-dot version isn't as commonly used as the two-dot version, and that extra dot can be easy to miss, making the code harder to maintain. I'd have to have a VERY good reason to use three-dots. Compare these outputs from IRB:

(1...2).to_a
=> [1]

vs.

(1..1).to_a
=> [1]

to see how misleading the first is.

Upvotes: 0

Andrew Grimm
Andrew Grimm

Reputation: 81510

If the code were properly indented, it'd be easier to notice that the constant definition was being done within a loop.

Compare

source.readlines.each do |line|
  # code
  if true

# Wrongly indented code

# More
# Wrongly
# Indented
# Code
EXTRACT_RANGES = [
  1...2
 ]

# Several more pages of code
  end
end

with

source.readlines.each do |line|
  # code
  if true

    # Correctly indented code

    # What is a constant doing being defined
    # this far indented?
    # Oh no - it's in a loop!

    EXTRACT_RANGES = [
      1...2
    ]

    # Several more pages of code
  end
end

Upvotes: 2

Moiz Raja
Moiz Raja

Reputation: 5760

Didn't notice earlier. Just move the constants out of the each block

EXTRACT_RANGES = [
  1...2
]

# Tags which count as delimiters, not to be extracted
DELIMITER_TAGS = [
 "h1",
 "h2",
 "h3"
]

source.readlines.each do |line|
 line.strip!
  if File.exists? line
    file = File.open(line)

doc = Nokogiri::HTML(File.read(line))

extracted_text = []

i = 0
# Change /"html"/"body" to the correct path of the tag which contains this list
(doc/"html"/"body").children.each do |el|

  if (DELIMITER_TAGS.include? el.name)
    i += 1
  else
    extract = false
    EXTRACT_RANGES.each do |cur_range|
      if (cur_range.include? i)
        extract = true
        break
      end
    end

    if extract
     s = el.inner_text.strip
      unless s.empty?
        extracted_text << el.inner_text.strip
      end
    end
  end
end

print("\n")
puts line
print(",\n")
# Print out extracted text (each element's inner text is separated by newlines)
puts extracted_text.join("\n\n")
  end
end

Upvotes: 1

Related Questions