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

  • Home
  • SEARCH
  • 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 7883565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:33:58+00:00 2026-06-03T04:33:58+00:00

In IRB, I’m trying the following: 1.9.3p194 :001 > foo = \xBF.encode(utf-8, :invalid =>

  • 0

In IRB, I’m trying the following:

1.9.3p194 :001 > foo = "\xBF".encode("utf-8", :invalid => :replace, :undef => :replace)
 => "\xBF" 
1.9.3p194 :002 > foo.match /foo/
ArgumentError: invalid byte sequence in UTF-8
from (irb):2:in `match'

Any ideas what’s going wrong?

  • 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-03T04:34:00+00:00Added an answer on June 3, 2026 at 4:34 am

    I’d guess that "\xBF" already thinks it is encoded in UTF-8 so when you call encode, it thinks you’re trying to encode a UTF-8 string in UTF-8 and does nothing:

    >> s = "\xBF"
    => "\xBF"
    >> s.encoding
    => #<Encoding:UTF-8>
    

    \xBF isn’t valid UTF-8 so this is, of course, nonsense. But if you use the three argument form of encode:

    encode(dst_encoding, src_encoding [, options] ) → str

    […] The second form returns a copy of str transcoded from src_encoding to dst_encoding.

    You can force the issue by telling encode to ignore what the string thinks its encoding is and treat it as binary data:

    >> foo = s.encode('utf-8', 'binary', :invalid => :replace, :undef => :replace)
    => "�"
    

    Where s is the "\xBF" that thinks it is UTF-8 from above.

    You could also use force_encoding on s to force it to be binary and then use the two-argument encode:

    >> s.encoding
    => #<Encoding:UTF-8>
    >> s.force_encoding('binary')
    => "\xBF"
    >> s.encoding
    => #<Encoding:ASCII-8BIT>
    >> foo = s.encode('utf-8', :invalid => :replace, :undef => :replace)
    => "�"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following: irb(main):001:0> class A irb(main):002:1> def initialize irb(main):003:2> @string = my string
C:\>irb irb(main):001:0> s = Proc.new { puts Hello } => #<Proc:0x04051780@(irb):1> irb(main):002:0> s.call Hello
Can anyone shed some insight onto why the following happens? irb(main):001:0> r = '1'..'30'
I am trying to run the following ruby code from IRB but am unable
I am trying to execute the following command in irb, exec 'gcc j.cpp' but,
irb(main):001:0> require 'active_support' => true irb(main):002:0> require 'active_support/inflector/inflections' => true irb(main):003:0> ActiveSupport::Inflector.pluralize('test') => test
Here's my irb session: irb(main):001:0> class User irb(main):002:1> include MongoMapper::Document irb(main):003:1> key :name, String
I'm trying compass in irb, this is my code: C:\Documents and Settings\test>irb irb(main):001:0> require
What is happening here?? irb(main):001:0> a= nil => nil irb(main):002:0> b = nil =>
irb(main):001:0> test = (0/1).rationalize => (0/1) irb(main):002:0> test.to_i NoMethodError: undefined method `to_i' for (0/1):Rational

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.