Removing newline from eachline in file

I'm reading a file line by line and trying to remove the newline at the
end of each line so that I can rebuild the url with the "www" and the
".net" at the beginning and end. Does anyone know how I can remove the
newline char at the end of each line? Also if the url doesn't work/exist
I don't want the script to crash I want to keep moving through the file
its reading.

begin
    while (line = f.readline)
        line = line =~ /(.*?)\n/
        url = "www." + line + ".net"

        open(url) { |page| page_content = page.read()
    puts "link exists"
  }
    end
rescue
puts "link does not exist"

rescue EOFError
    f.close
end

···

--
Posted via http://www.ruby-forum.com/.

Chuck Dawit wrote:

I'm reading a file line by line and trying to remove the newline at the
end of each line so that I can rebuild the url with the "www" and the
".net" at the beginning and end. Does anyone know how I can remove the
newline char at the end of each line?

See String#chomp:

require "open-uri"

File.open("aaa.txt", "w") do |file|
    file.print("hello\n", "world\n", "goodbye\n")
end

File.open("aaa.txt") do |file|
    file.each do |line|
        print line.chomp
    end
    puts
end

--output:--
helloworldgoodbye

···

--
Posted via http://www.ruby-forum.com/\.

Chuck Dawit wrote:

I'm reading a file line by line and trying to remove the newline at the
end of each line so that I can rebuild the url with the "www" and the
".net" at the beginning and end. Does anyone know how I can remove the
newline char at the end of each line? Also if the url doesn't work/exist
I don't want the script to crash I want to keep moving through the file
its reading.

  ri String#chomp
----------------------------------------------------------- String#chomp
      str.chomp(separator=$/) => new_str

···

------------------------------------------------------------------------
      Returns a new String with the given record separator removed from
      the end of str (if present). If $/ has not been changed from the
      default Ruby record separator, then chomp also removes carriage
      return characters (that is it will remove \n, \r, and \r\n).

         "hello".chomp #=> "hello"
         "hello\n".chomp #=> "hello"
         "hello\r\n".chomp #=> "hello"
         "hello\n\r".chomp #=> "hello\n"
         "hello\r".chomp #=> "hello"
         "hello \n there".chomp #=> "hello \n there"
         "hello".chomp("llo") #=> "he"

--
RMagick OS X Installer [http://rubyforge.org/projects/rmagick/\]
RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?forum_id=1618\]
RMagick Installation FAQ [http://rmagick.rubyforge.org/install-faq.html\]

Chuck Dawit wrote:

Also if the url doesn't work/exist
I don't want the script to crash I want to keep moving through the file
its reading.

My isp redirects faulty urls to an error page, so all urls return a web
page. I think you're going to have to use Net::HTTP to look at the
headers of the response, and then take some action based on the header
value.

···

--
Posted via http://www.ruby-forum.com/\.

Chuck Dawit wrote:

Also if the url doesn't work/exist
I don't want the script to crash I want to keep moving through the file
its reading.

begin
    while (line = f.readline)
        line = line =~ /(.*?)\n/
        url = "www." + line + ".net"

        open(url) { |page| page_content = page.read()
    puts "link exists"
  }
    end
rescue
puts "link does not exist"

rescue EOFError
    f.close
end

If your program is currently crashing on bad urls, you just need to put
the begin/rescue around the open statement---not outside your loop.
Something like this(untested):

begin
    while (line = f.readline)
        line = line =~ /(.*?)\n/
        url = "www." + line + ".net"

        begin
               open(url) { |page| page_content = page.read() }
               puts "link exists"
        rescue Exception
               puts "in rescue"
               #do nothing -- when you catch an exception the exception
goes away

        puts "execution continues on its way"
    end
end

···

--
Posted via http://www.ruby-forum.com/\.

Try changing the code to:
       file_in = File.new("whatever")
       file_in.each do |x|
             url = x.chomp + ".net"

gud luck
            
Chuck Dawit wrote:

···

I'm reading a file line by line and trying to remove the newline at the
end of each line so that I can rebuild the url with the "www" and the
".net" at the beginning and end. Does anyone know how I can remove the
newline char at the end of each line? Also if the url doesn't work/exist
I don't want the script to crash I want to keep moving through the file
its reading.

begin
    while (line = f.readline)
        line = line =~ /(.*?)\n/
        url = "www." + line + ".net"

        open(url) { |page| page_content = page.read()
    puts "link exists"
  }
    end
rescue
puts "link does not exist"

rescue EOFError
    f.close
end

# line = line =~ /(.*?)\n/

what does this line of code do?