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 9133265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:27:22+00:00 2026-06-17T08:27:22+00:00

I have some simple text processing scripts I use all the time, and I

  • 0

I have some simple text processing scripts I use all the time, and I want to translate those scripts into Ruby, to get familiar with the language.

This is the first script I can’t get to run:

#!/usr/bin/env ruby

@text = ARGF.read

@replacements = [{:from=>"—", :to=>". "}, {:from=>"ffl", :to=>"ffl"}, {:from=>"ffi", :to=>"ffi"}, {:from=>"fi", :to=>"fi"}, {:from=>"fl", :to=>"fl"}, {:from=>"ff", :to=>"ff"}, {:from=>"æ", :to=>"ae"}, {:from=>"é", :to=>"e"}, {:from=>"Ç", :to=>"s"}, {:from=>"ü", :to=>"u"}, {:from=>"â", :to=>"a"}, {:from=>"ä", :to=>"a"}, {:from=>"à", :to=>"a"}, {:from=>"å", :to=>"a"}, {:from=>"ç", :to=>"s"}, {:from=>"ê", :to=>"e"}, {:from=>"ë", :to=>"e"}, {:from=>"è", :to=>"e"}, {:from=>"ï", :to=>"i"}, {:from=>"î", :to=>"i"}, {:from=>"ì", :to=>"i"}, {:from=>"Ä", :to=>"a"}, {:from=>"Å", :to=>"a"}, {:from=>"É", :to=>"e"}, {:from=>"ô", :to=>"oh"}, {:from=>"ö", :to=>"oe"}, {:from=>"ò", :to=>"o"}, {:from=>"û", :to=>"uu"}, {:from=>"ù", :to=>"u"}, {:from=>"ÿ", :to=>"o"}, {:from=>"Ö", :to=>"o"}, {:from=>"Ü", :to=>"u"}, {:from=>"á", :to=>"ah"}, {:from=>"í", :to=>"ee"}, {:from=>"ó", :to=>"oh"}, {:from=>"ú", :to=>"uu"}, {:from=>"ñ", :to=>"ny"}, {:from=>"Ñ", :to=>"ny"}] 

@replacements.each do |pair|
  @text.gsub!(/#{pair[:from]}/, pair[:to])
end

puts @text

And here’s the error I get:

/home/alec/.bei/under-boac:5: invalid multibyte char (US-ASCII)
/home/alec/.bei/under-boac:5: invalid multibyte char (US-ASCII)
/home/alec/.bei/under-boac:5: syntax error, unexpected $end, expecting '}'
@replacements = [{:from=>"—", :to=>". "}, {:from=>"ffl"...
                            ^

I based part of it on “Best practices with STDIN in Ruby?“.

  • 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-17T08:27:24+00:00Added an answer on June 17, 2026 at 8:27 am

    This is your base code, reformatted for readability:

    @replacements = [
      { :from => "—", :to => ". "  },
      { :from => "ffl", :to => "ffl" },
      { :from => "ffi", :to => "ffi" },
      { :from => "fi", :to => "fi"  },
      { :from => "fl", :to => "fl"  },
      { :from => "ff", :to => "ff"  },
      { :from => "æ", :to => "ae"  },
      { :from => "é", :to => "e"   },
      { :from => "Ç", :to => "s"   },
      { :from => "ü", :to => "u"   },
      { :from => "â", :to => "a"   },
      { :from => "ä", :to => "a"   },
      { :from => "à", :to => "a"   },
      { :from => "å", :to => "a"   },
      { :from => "ç", :to => "s"   },
      { :from => "ê", :to => "e"   },
      { :from => "ë", :to => "e"   },
      { :from => "è", :to => "e"   },
      { :from => "ï", :to => "i"   },
      { :from => "î", :to => "i"   },
      { :from => "ì", :to => "i"   },
      { :from => "Ä", :to => "a"   },
      { :from => "Å", :to => "a"   },
      { :from => "É", :to => "e"   },
      { :from => "ô", :to => "oh"  },
      { :from => "ö", :to => "oe"  },
      { :from => "ò", :to => "o"   },
      { :from => "û", :to => "uu"  },
      { :from => "ù", :to => "u"   },
      { :from => "ÿ", :to => "o"   },
      { :from => "Ö", :to => "o"   },
      { :from => "Ü", :to => "u"   },
      { :from => "á", :to => "ah"  },
      { :from => "í", :to => "ee"  },
      { :from => "ó", :to => "oh"  },
      { :from => "ú", :to => "uu"  },
      { :from => "ñ", :to => "ny"  },
      { :from => "Ñ", :to => "ny"  }
    ] 
    
    @replacements.each do |pair|
      @text.gsub!( /#{ pair[:from] }/, pair[:to] )
    end
    

    That can be simplified, and the hashes combined to be one big one, and, incidently, be used as a hash should be used:

    # encoding: utf-8
    
    @replacements = {
      "—" => ". "  ,
      "ffl" => "ffl" ,
      "ffi" => "ffi" ,
      "fi" => "fi"  ,
      "fl" => "fl"  ,
      "ff" => "ff"  ,
      "æ" => "ae"  ,
      "é" => "e"   ,
      "Ç" => "s"   ,
      "ü" => "u"   ,
      "â" => "a"   ,
      "ä" => "a"   ,
      "à" => "a"   ,
      "å" => "a"   ,
      "ç" => "s"   ,
      "ê" => "e"   ,
      "ë" => "e"   ,
      "è" => "e"   ,
      "ï" => "i"   ,
      "î" => "i"   ,
      "ì" => "i"   ,
      "Ä" => "a"   ,
      "Å" => "a"   ,
      "É" => "e"   ,
      "ô" => "oh"  ,
      "ö" => "oe"  ,
      "ò" => "o"   ,
      "û" => "uu"  ,
      "ù" => "u"   ,
      "ÿ" => "o"   ,
      "Ö" => "o"   ,
      "Ü" => "u"   ,
      "á" => "ah"  ,
      "í" => "ee"  ,
      "ó" => "oh"  ,
      "ú" => "uu"  ,
      "ñ" => "ny"  ,
      "Ñ" => "ny"  
    }
    @replacements.each do |k,v|
      @text.gsub!(k, v)
    end
    

    Note: use the “encoding comment” to help Ruby understand the encoding of the characters.

    But using the same hash, that loop can be reduced down to this, which runs extremely fast:

    @text.gsub!(Regexp.union(@replacements.keys), @replacements)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

CSS I have some simple CSS to apply a negative text-indent value on the
I have HTML + CSS text. I need some lib with simple function like
Pretty simple question. I have a few ASP RequiredFieldValdators checking some text boxes. Out
I have some simple JQuery / Javascript to perform some simple logic for all
I have some simple constraints involving multiplication of reals in z3 that are producing
I have some simple markup: <table> <tr> <td>Menu Item</td> </tr> </table> <div> <table> <tr>
i have some WinForms app (Framework to develop some simple apps), written in C#.
i have some WinForms app (Framework to develop some simple apps), written in C#.
Let's say I have some simple Javascript like: <script> var hello = function(){ alert(Hello
Let's say I have some simple data y = [[datetime.datetime( 2012,1,1,1,1), 2.1], [datetime.datetime( 2012,1,1,1,2),

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.