Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9031005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:37:43+00:00 2026-06-16T07:37:43+00:00

Possible Duplicate: ruby 1.9: invalid byte sequence in UTF-8 I’m currently building a file

  • 0

Possible Duplicate:
ruby 1.9: invalid byte sequence in UTF-8

I’m currently building a file system crawler and receiving the following error when running my script:

wordcrawler.rb:8:in `block in <main>': invalid byte sequence in UTF-8 (ArgumentError)
    from /Users/Anconia/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/find.rb:41:in `block in find'
    from /Users/Anconia/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/find.rb:40:in `catch'
    from /Users/Anconia/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/find.rb:40:in `find'
    from wordcrawler.rb:5:in `<main>'

And here is my code:

require 'find'

count = 0

Find.find('/Users/Anconia/') do |file|                   # '/' for root directory on OS X
  if file =~ /\b(\.txt|\.doc|\.docx)\b/                # check if filename ends in desired format
    contents = File.read(file)
      if contents =~ /regex/
      puts file
      count += 1
    end
  end
end

puts "#{count} files were found"

In my dev environment I use ruby 1.9.3; however, when I switch to ruby 1.8.7 the script runs properly. And I’d like to continue using 1.9.3 if possible. I’ve tried every solution in this post (ruby 1.9: invalid byte sequence in UTF-8) but my problems still persist. Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T07:37:44+00:00Added an answer on June 16, 2026 at 7:37 am

    Wasn’t properly understanding the contents of the aforementioned post. At bare minimum this can be used as an implemented example of this post

    require 'find'
    
    count = 0
    
    Find.find('/Users/Anconia/') do |file|                                              # '/' for root directory on OS X
      if file =~ /\b(\.txt|\.doc|\.docx)\b/                                           # check if filename ends in desired format
        contents = File.read(file).encode!('UTF-8', 'UTF-8', :invalid => :replace)    # resolves encoding errors - must use 1.9.3 else use iconv
          if contents =~ /regex/
            puts file
            count += 1
        end
      end
    end
    
    puts "#{count} files were found"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Perplexing Ruby/MySQL Error: "invalid packet: sequence number mismatch" My rails app just
Possible Duplicate: Ruby - UTF-8 file encoding I'm using UTF-8 all the way and
Possible Duplicate: Hex to binary in ruby In Python, I can do the following:
Possible Duplicate: Ruby view csv data In my app i'm reading csv file, but
Possible Duplicate: Test if string is a number in Ruby on Rails Currently I
Possible Duplicate: How to remove rvm (ruby version manager) from my system? How can
Possible Duplicate: Ruby 1.9 - no such file to load 'win32/open3' I am having
Possible Duplicate: What is the difference between include and require in Ruby? require File.join(File.dirname(__FILE__),
Possible Duplicate: Ruby On Rails 3 and Webrick issue $ rails server /Users/Vineeth/.rvm/gems/ruby-1.9.3-p194/gems/ruby-debug-base19-0.11.25/lib/ruby-debug-base.rb:1:in `require':
Possible Duplicate: Ruby syntax question: Rational(a, b) and Rational.new!(a, b) I'm in the process

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.