# Argument error --- How to solve?

**URL:** <https://rubytalk.org/t/argument-error-how-to-solve/64097>\
**Category:** ruby-talk\
**Created:** [9 November 2011 20:35 UTC](https://rubytalk.org/t/argument-error-how-to-solve/64097 "2011-11-09T20:35:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ar\_Ik](https://avatars.discourse-cdn.com/v4/letter/a/cc9497/32.png) [@Ar\_Ik](https://rubytalk.org/u/Ar_Ik)\
**Post date:** [9 November 2011 20:35 UTC](https://rubytalk.org/t/argument-error-how-to-solve/64097/1 "2011-11-09T20:35:54Z")

</div>

I´m at the very beginning with Ruby and give again and again this error

program.rb:6:in `gsub': broken utf-8 string (argumenterror)

when I'm trying this short code:

#coding:utf-8

temp=""  
txtfile=File.open("8-3\_tiedosto.txt","r");txtfile.each{|row|temp=temp+row};txtfile.close

temp = temp.gsub("Å", '')  
puts temp

The original text in the file contains characters that I do not to  
include to my final result, that should only contain ASCII 65..90 and  
97..122. So I do not understand, what arguments should be given to gsub?

I'm sorry because of my stupidity 🙂

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Carina\_C\_Zona](https://avatars.discourse-cdn.com/v4/letter/c/ea5d25/32.png) [@Carina\_C\_Zona](https://rubytalk.org/u/Carina_C_Zona)\
**Post date:** [10 November 2011 04:20 UTC](https://rubytalk.org/t/argument-error-how-to-solve/64097/2 "2011-11-10T04:20:27Z")

</div>

Your first argument to gsub appears to be ASCII 197.

> **···**
>
> On Wed, Nov 9, 2011 at 12:35 PM, Ar Ik \<arsi.ikonen@gmail.com\> wrote:
> 
> > I´m at the very beginning with Ruby and give again and again this error
> > 
> > program.rb:6:in `gsub': broken utf-8 string (argumenterror)
> > 
> > when I'm trying this short code:
> > 
> > #coding:utf-8
> > 
> > temp=""  
> > txtfile=File.open("8-3\_tiedosto.txt","r");txtfile.each{|row|temp=temp+row};txtfile.close
> > 
> > temp = temp.gsub("Å", '')  
> > puts temp
> > 
> > The original text in the file contains characters that I do not to  
> > include to my final result, that should only contain ASCII 65..90 and  
> > 97..122. So I do not understand, what arguments should be given to gsub?
> > 
> > I'm sorry because of my stupidity 🙂
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> Carina

---

<div class="post-metadata">

**Author:** ![Ar\_Ik](https://avatars.discourse-cdn.com/v4/letter/a/cc9497/32.png) [@Ar\_Ik](https://rubytalk.org/u/Ar_Ik)\
**Post date:** [10 November 2011 08:03 UTC](https://rubytalk.org/t/argument-error-how-to-solve/64097/3 "2011-11-10T08:03:13Z")

</div>

Not possile to edit previous, so additional comment: My environment do  
not allow to change character statement at the first line...

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Nik\_Z](https://avatars.discourse-cdn.com/v4/letter/n/46a35a/32.png) [@Nik\_Z](https://rubytalk.org/u/Nik_Z)\
**Post date:** [10 November 2011 22:54 UTC](https://rubytalk.org/t/argument-error-how-to-solve/64097/4 "2011-11-10T22:54:44Z")

</div>

--Try doing this and see if it helps with your substitution experience,  
without getting too involved with Ruby's encoding mechanism

#coding:utf-8

## Do NOT delete the above utf-8 line, which  
## you already have in your original copy

temp=""  
txtfile=File.open("8-3\_tiedosto.txt","r")  
txtfile.each{|row|temp=temp+row}  
txtfile.close

tmp = temp.gsub(/[^A-Z0-9[:punct:]\s]+/ix, '')

puts tmp

PS--I left the numericals and all kinds of punctuational marks in there,  
just in case if you have them in the original file--though there are  
certainly not within your original range of ASCII 65..90 and 97..122

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Ar\_Ik](https://avatars.discourse-cdn.com/v4/letter/a/cc9497/32.png) [@Ar\_Ik](https://rubytalk.org/u/Ar_Ik)\
**Post date:** [10 November 2011 07:14 UTC](https://rubytalk.org/t/argument-error-how-to-solve/64097/5 "2011-11-10T07:14:06Z")

</div>

C. Zona wrote in post #1031196:

> Your first argument to gsub appears to be ASCII 197.

Yes, You're correct, but still I do not know how to fix my code...As the  
source text contains chars not among 65..90 and 97..122, how I can  
remove or replace them?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [10 November 2011 09:03 UTC](https://rubytalk.org/t/argument-error-how-to-solve/64097/6 "2011-11-10T09:03:12Z")

</div>

If you insert this line at the beginning of the script, what does it print?

p("".encoding)

Btw, you can simplify reading by doing

txtfile = File.read("8-3\_tiedosto.txt", encoding: 'UTF-8')

assuming your file is encoded in UTF-8.

You might have to play with  
Encoding.default\_external=  
Encoding.default\_internal=

For more please see

> **[Gray Soft / Not Found](http://graysoftinc.com/articles/miscellaneous_m17n_details)**
>
> Something has gone wrong.

Kind regards

robert

> **···**
>
> On Thu, Nov 10, 2011 at 9:03 AM, Ar Ik \<arsi.ikonen@gmail.com\> wrote:
> 
> > Not possile to edit previous, so additional comment: My environment do  
> > not allow to change character statement at the first line...
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Luca\_Email](https://avatars.discourse-cdn.com/v4/letter/l/76d3ee/32.png) [@Luca\_Email](https://rubytalk.org/u/Luca_Email)\
**Post date:** [29 December 2011 06:58 UTC](https://rubytalk.org/t/argument-error-how-to-solve/64097/7 "2011-12-29T06:58:13Z")

</div>

-----Messaggio originale-----

> **···**
>
> Da: Nik Z. [[mailto:esperantoca@gmail.com](mailto:esperantoca@gmail.com)]  
> Inviato: giovedì 10 novembre 2011 23:55  
> A: ruby-talk ML  
> Oggetto: Re: Argument error --- How to solve?
> 
> --Try doing this and see if it helps with your substitution experience,  
> without getting too involved with Ruby's encoding mechanism
> 
> #coding:utf-8
> 
> ## Do NOT delete the above utf-8 line, which ## you already have in your  
> original copy
> 
> temp=""  
> txtfile=File.open("8-3\_tiedosto.txt","r")  
> txtfile.each{|row|temp=temp+row}  
> txtfile.close
> 
> tmp = temp.gsub(/[^A-Z0-9[:punct:]\s]+/ix, '')
> 
> puts tmp
> 
> PS--I left the numericals and all kinds of punctuational marks in there,  
> just in case if you have them in the original file--though there are  
> certainly not within your original range of ASCII 65..90 and 97..122
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> --  
> Caselle da 1GB, trasmetti allegati fino a 3GB e in piu' IMAP, POP3 e SMTP autenticato? GRATIS solo con [Email.it](http://Email.it) [http://www.email.it/f](http://www.email.it/f)
> 
> Sponsor:  
> Conto Arancio al 4,20%. Zero spese e massima liberta', aprilo in due minuti!  
> Clicca qui: [http://adv.email.it/cgi-bin/foclick.cgi?mid](http://adv.email.it/cgi-bin/foclick.cgi?mid)922&d)-12

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [10 November 2011 20:41 UTC](https://rubytalk.org/t/argument-error-how-to-solve/64097/8 "2011-11-10T20:41:29Z")

</div>

Ar Ik wrote in post #1031208:

> C. Zona wrote in post #1031196:
> 
> > Your first argument to gsub appears to be ASCII 197.
> 
> Yes, You're correct, but still I do not know how to fix my code...As the  
> source text contains chars not among 65..90 and 97..122, how I can  
> remove or replace them?

Strings in ruby 1.9 are complicated beasts. I had a go at understanding  
them:

> <https://github.com/candlerb/string19/blob/master/string19.rb>

So it really depends on what you're trying to do. If you want to  
manipulate this file as a series of bytes, and match particular bytes,  
then open it in binary mode ('rb'), and pass only binary strings to  
gsub.

&nbsp;&nbsp;temp.gsub!("xxx".force\_encoding("BINARY"), "")

The trouble with opening the file as UTF-8, and doing regexp matches  
with UTF-8 characters, is that your program will crash when fed invalid  
UTF-8 data. So it is not good for "data cleaning" exercises.

But strangely, ruby 1.9 is quite happy to deal with invalid strings in  
some contexts. For example, if you do

&nbsp;&nbsp;&nbsp;temp.size.times do |i|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts temp[i]  
&nbsp;&nbsp;&nbsp;end

then it will work even if the i'th character is invalid. Go figure.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Luca\_Email](https://avatars.discourse-cdn.com/v4/letter/l/76d3ee/32.png) [@Luca\_Email](https://rubytalk.org/u/Luca_Email)\
**Post date:** [29 December 2011 10:35 UTC](https://rubytalk.org/t/argument-error-how-to-solve/64097/9 "2011-12-29T10:35:20Z")

</div>

-----Messaggio originale-----

> **···**
>
> Da: Luca (Email) [[mailto:luca.pagano@email.it](mailto:luca.pagano@email.it)]  
> Inviato: giovedì 29 dicembre 2011 07:58  
> A: ruby-talk ML  
> Oggetto: I: Argument error --- How to solve?
> 
> --  
> Caselle da 1GB, trasmetti allegati fino a 3GB e in piu' IMAP, POP3 e SMTP autenticato? GRATIS solo con [Email.it](http://Email.it) [http://www.email.it/f](http://www.email.it/f)
> 
> Sponsor:  
> Riccione Hotel 3 stelle in centro: Pacchetto Capodanno mezza pensione, animazione bimbi, zona relax, parcheggio. Scopri l'offerta solo per oggi...  
> Clicca qui: [http://adv.email.it/cgi-bin/foclick.cgi?mid](http://adv.email.it/cgi-bin/foclick.cgi?mid)983&d)-12
