[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Saving an UTF-8 file

Miquel Oliete

11/12/2006 10:17:00 AM

Hi All

I have a problem (newbie problem).

I don't know how to write a file using utf-8 encoding. Can you help
me.

Thanks in advance

Kind regards

--

Miquel (a.k.a. Ton)
Linux User #286784
GPG Key : 4D91EF7F
Debian GNU/Linux (Linux Wolverine 2.6.14)

Welcome to the jungle, we got fun and games
Guns n' Roses


______________________________________________
LLama Gratis a cualquier PC del Mundo.
Llamadas a fijos y m?viles desde 1 c?ntimo por minuto.
http://es.voice...

14 Answers

Paul Lutus

11/12/2006 11:24:00 AM

0

Miquel Oliete wrote:

> Hi All
>
> I have a problem (newbie problem).
>
> I don't know how to write a file using utf-8 encoding. Can you help
> me.

utf-8 is simply 8-bit bytes. Save your data like this:

# "data" contain the text data

File.open(file_path,"w") { |f| f.write data }

utf-8 refers to a convention regarding the content of the bytes and how they
are interpreted when read. It isn't something you can specify in a
plain-text file. It can be inferred from the format of the bytes, but that
is an open interpretation.

http://dict.die....

--
Paul Lutus
http://www.ara...

David Vallner

11/12/2006 1:01:00 PM

0

Paul Lutus wrote:
> It isn't something you can specify in a
> plain-text file.

Byte order mark?

A specification it is not, but generally a good hint. There are gotchas
though if you process it with software that's not Unicode-unaware.

David Vallner

Austin Ziegler

11/12/2006 4:20:00 PM

0

On 11/12/06, David Vallner <david@vallner.net> wrote:
> Paul Lutus wrote:
> > It isn't something you can specify in a
> > plain-text file.
> Byte order mark?

Not meaningful in UTF-8, since it's all a defined series of bytes
(it's always the same order on all platforms).

-austin
--
Austin Ziegler * halostatue@gmail.com * http://www.halo...
* austin@halostatue.ca * http://www.halo...feed/
* austin@zieglers.ca

David Vallner

11/12/2006 4:44:00 PM

0

Austin Ziegler wrote:
> On 11/12/06, David Vallner <david@vallner.net> wrote:
>> Paul Lutus wrote:
>> > It isn't something you can specify in a
>> > plain-text file.
>> Byte order mark?
>
> Not meaningful in UTF-8, since it's all a defined series of bytes
> (it's always the same order on all platforms).
>
> -austin

Yes, but it can be used as a "this file is UTF-8" marker by convention.
And cause problems in software that doesn't recognize the convention,
for added hilarity.

David Vallner

Austin Ziegler

11/12/2006 5:06:00 PM

0

On 11/12/06, David Vallner <david@vallner.net> wrote:
> Austin Ziegler wrote:
> > On 11/12/06, David Vallner <david@vallner.net> wrote:
> >> Paul Lutus wrote:
> >> > It isn't something you can specify in a
> >> > plain-text file.
> >> Byte order mark?
> > Not meaningful in UTF-8, since it's all a defined series of bytes
> > (it's always the same order on all platforms).
> Yes, but it can be used as a "this file is UTF-8" marker by convention.
> And cause problems in software that doesn't recognize the convention,
> for added hilarity.

It's a bad convention, because it adds meaningless bytes to the
beginning of a file. I'm not saying that an unadorned document is
better, but better to do something that has actual meaning than doing
a pointless BOM.

-austin
--
Austin Ziegler * halostatue@gmail.com * http://www.halo...
* austin@halostatue.ca * http://www.halo...feed/
* austin@zieglers.ca

Dido Sevilla

11/12/2006 10:14:00 PM

0

On 11/12/06, Miquel Oliete <ktalanet@yahoo.es> wrote:
> Hi All
>
> I have a problem (newbie problem).
>
> I don't know how to write a file using utf-8 encoding. Can you help
> me.

Well, how are you storing the Unicode characters are you using
internally? If your Unicode string within Ruby is stored as an array
of ints, then

File.open("output_file.utf8") do |fp|
fp.puts(data.pack("U*"))
end

should be sufficient. If you have a Ruby string that uses some other
encoding (e.g. ISO-8859-1), then you must use the iconv library to
convert the string to UTF-8:

require 'iconv'

cd = Iconv.new('utf-8', 'iso-8859-1')
File.open("output_file.utf8") do |fp|
fp.puts(cd.iconv(data))
end

When you do i18n, l10n, and m17n, strings become meaningless unless
they have an attached encoding.

ALP Fomenting Debt

12/24/2012 7:37:00 PM

0

On 24/12/2012 2:51 PM, David Moss wrote:
> On Mon, 24 Dec 2012 07:02:48 +1100, ALP = Abusive Liars Party
> <gmsmerkoff@carbon.tax.is.ineffective> wrote:
>
>> "...Rudd visited Indonesia, the US, France, Belgium, UK, Germany,
>> Ethiopia and Switzerland, Mexico and Tunisia on a whirlwind tour that
>> included the 48th Munich Security Conference".
>
> "A less noted development in Australian foreign policy this year has
> been the evolution of Kevin Rudd's ideas on the future of the Asian
> order and the US-China relationship. Since an address to the Asia
> Society in New York in January, Rudd has delivered a series of
> speeches around the world in which he has set out a pretty robust and
> clear model for the future Asian order."
>
> Hmm, most people would consider Australia having significant input to
> geopolitical evolution to be a good thing.

Actually I don't mind him as Foreign Minister or as PM - if only he
could control his spending.

I just wish that the ALP would decide who it is who *is* the foreign
minister.

>
> Whats Tony been doing lately? Scrounging through the rubbish bin at a
> lawyers office looking for dirt on Gillard? Now thats the kind of
> bloke we need in parliament!

Silly isn't it? All he has to do is ask Nicola.

>
>> But constituents ain't seen no hair nor hide of him in Griffith...
>
> The constituents partying with him at a West End restaurant in
> Brisbane last Wednesday night seemed happy with him.
>
> I wonder what whats-his-name-again, the LNP bloke was doing ?
>

I give up - Not spending taxpayers money?

So your philosophy is to vote for people if they are well known. Pretty
shallow really.


>
>
>
>
>
> DM
> personal opinion only
> ---------------------
> APR - http://politics.sunnybar...
>


--
Gillard - one week before abandoning the faux surplus...
"We don't jump at people's figments of imagination ? we deal with the
facts, we deal with the forecasts, and I refer you to what we said in
that mid-year [budget] statement"

Swan - one week later...abandoning the surplus...
"The course of action I'm talking about today really comes very much, I
guess, to the core of my values and the values of the Labor Party"
(rotfl - that they can't balance a budget?)

Abbott...
"If the Labor Party thinks there's been some terrible injustice done to
Mr Slipper,
they should put him back into the Speaker's chair"

Paul Howes...
"Labor has an obligation to stop extremists who threaten our democracy"

Tanner...
"When I joined the Labor Party people joined Labor to change the world.
Now they join Labor to run the world".

How much new law is there every year?
http://www.youtube.com/watch?v=l...

Gillard - who do you believe?
"The Prime Minister of Australia ...?"

David Moss

12/24/2012 11:15:00 PM

0

On Tue, 25 Dec 2012 06:36:44 +1100, ALP = Abusive Liars Party
<gmsmerkoff@carbon.tax.is.ineffective> wrote:

>> Hmm, most people would consider Australia having significant input to
>> geopolitical evolution to be a good thing.
>
>Actually I don't mind him as Foreign Minister or as PM - if only he
>could control his spending.

If you do a lot of work you run up a lot of costs. The test is whether
the benefits acrued outway the costs incurred. We will have to wait
for history to find out.

>I just wish that the ALP would decide who it is who *is* the foreign
>minister.

That was Bob last time I checked. Bob Carr is the Minister responsible
for foreign affairs. Its his job to manage foreign relations on behalf
of Australians. Mnagement is the process of dealing with or
controlling things or people. A manager adds value to the organisation
through the efforts of others. Rudd, for example.

The current crop of Liberals don't understand management. They are
obsessed with position and think the leader should do everything
personally. That concept doesn't scale past branch level.

>> Whats Tony been doing lately? Scrounging through the rubbish bin at a
>> lawyers office looking for dirt on Gillard? Now thats the kind of
>> bloke we need in parliament!
>
>Silly isn't it? All he has to do is ask Nicola.

Tony is just doing what he has always done. Sometimes it works (as
with Pauline Hanson), sometimes it just makes him look like a goose.

I shudder to think what would happen if he went through this routine
in geopolitics.

>>> But constituents ain't seen no hair nor hide of him in Griffith...
>>
>> The constituents partying with him at a West End restaurant in
>> Brisbane last Wednesday night seemed happy with him.
>>
>> I wonder what whats-his-name-again, the LNP bloke was doing ?
>>
>
>I give up - Not spending taxpayers money?

I doubt that. But then I know how the Libs fund their campaigns. (How
every Party in government funds its campaigns)

>So your philosophy is to vote for people if they are well known. Pretty
>shallow really.

My philisophy is not to vote for people I think are going to harm me,
my family and my country. Once I've eliminated the obvious I consider
who to vote for from those that remain. I'd really love to vote
Liberal again, but I won't while Tony heads the lineup. Even Malcolm
would be better.


DM
personal opinion only
---------------------
APR - http://politics.sunnybar...

ALP Fomenting Debt

12/25/2012 10:09:00 PM

0

On 25/12/2012 10:15 AM, David Moss wrote:
> On Tue, 25 Dec 2012 06:36:44 +1100, ALP = Abusive Liars Party
> <gmsmerkoff@carbon.tax.is.ineffective> wrote:
>
>>> Hmm, most people would consider Australia having significant input to
>>> geopolitical evolution to be a good thing.
>>
>> Actually I don't mind him as Foreign Minister or as PM - if only he
>> could control his spending.
>
> If you do a lot of work you run up a lot of costs. The test is whether
> the benefits acrued outway the costs incurred. We will have to wait
> for history to find out.

Normally financially rational people do a business case first.

A lot of activity also amounts to 'brownian motion' - pressure in all
directions and no forward direction.

Thank you for an excellent metaphor of the Rudd era.


>
>> I just wish that the ALP would decide who it is who *is* the foreign
>> minister.
>
> That was Bob last time I checked. Bob Carr is the Minister responsible
> for foreign affairs. Its his job to manage foreign relations on behalf
> of Australians. Mnagement is the process of dealing with or
> controlling things or people. A manager adds value to the organisation
> through the efforts of others. Rudd, for example.

Officially he is but he hasn't achieved anything of significance and he
wasn't elected, so I have serious doubts about him representing us.

>
> The current crop of Liberals don't understand management. They are
> obsessed with position and think the leader should do everything
> personally. That concept doesn't scale past branch level.
>
>>> Whats Tony been doing lately? Scrounging through the rubbish bin at a
>>> lawyers office looking for dirt on Gillard? Now thats the kind of
>>> bloke we need in parliament!
>>
>> Silly isn't it? All he has to do is ask Nicola.
>
> Tony is just doing what he has always done. Sometimes it works (as
> with Pauline Hanson), sometimes it just makes him look like a goose.

Ho-hum. 'Abbott Abbott Abbott'

The investigation into the fraud/incompetence of the PM was not
initiated nor led by Abbott. The fact that you say so illustrates that
you've succumbed to the propaganda and you're a mark.

Why cannot Julia Gillard answer some straightforward questions?

Unionists with a brain would by now be realising that they'd be better
off with a government that doesn't destroy jobs and doesn't ramp up
immigration and makes it easy to do business, which in turn creates jobs.

Female unemployment has risen under Rudd/Gillard's faux feminism and
covert ultra-leftism - as it always does. Women want and need part-time
jobs and flexible jobs & Gillard is instating rigid union rules and
regulations - the result is loss of jobs. Women's unemployment has gone
from 4.4% to 5.2% under Gillard. Real unemployment has double since 2007
from 600,000 to 1.2m and yet the Gillard ALP has ramped up immigration
by 25% *despite* promising a 'population debate' without hurtling toward
a 'big Australia'.

Tony Abbott is honest and his volunteering shows where his heart is.

The campaign by the ALP to demonise him is typical ALP ad-hom politics.


>
> I shudder to think what would happen if he went through this routine
> in geopolitics.

What routine?

A union official starts defrauding the unions and its members and you
get suckered into a belief that it is due to Tony Abbott?
Jeez, you are delusional!

>
>>>> But constituents ain't seen no hair nor hide of him in Griffith...
>>>
>>> The constituents partying with him at a West End restaurant in
>>> Brisbane last Wednesday night seemed happy with him.
>>>
>>> I wonder what whats-his-name-again, the LNP bloke was doing ?
>>>
>>
>> I give up - Not spending taxpayers money?
>
> I doubt that. But then I know how the Libs fund their campaigns. (How
> every Party in government funds its campaigns)
>
>> So your philosophy is to vote for people if they are well known. Pretty
>> shallow really.
>
> My philisophy is not to vote for people I think are going to harm me,
> my family and my country. Once I've eliminated the obvious I consider
> who to vote for from those that remain.

Well mine is similar. I eliminate everyone who lies - which is why I
campaigned against Howard.

> I'd really love to vote
> Liberal again, but I won't while Tony heads the lineup. Even Malcolm
> would be better.

And everything you say about Tony Abbott is fed to you by the ALP
propaganda unit.


>
>
> DM
> personal opinion only
> ---------------------
> APR - http://politics.sunnybar...
>


--
Gillard - one week before abandoning the faux surplus...
"We don't jump at people's figments of imagination ? we deal with the
facts, we deal with the forecasts, and I refer you to what we said in
that mid-year [budget] statement"

Swan - one week later...abandoning the surplus...
"The course of action I'm talking about today really comes very much, I
guess, to the core of my values and the values of the Labor Party"
(rotfl - that they can't balance a budget?)

Abbott...
"If the Labor Party thinks there's been some terrible injustice done to
Mr Slipper,
they should put him back into the Speaker's chair"

Paul Howes...
"Labor has an obligation to stop extremists who threaten our democracy"

Tanner...
"When I joined the Labor Party people joined Labor to change the world.
Now they join Labor to run the world".

How much new law is there every year?
http://www.youtube.com/watch?v=l...

Gillard - who do you believe?
"The Prime Minister of Australia ...?"

David Moss

12/26/2012 8:07:00 AM

0

On Wed, 26 Dec 2012 09:09:11 +1100, ALP = Abusive Liars Party
<gmsmerkoff@carbon.tax.is.ineffective> wrote:

>> If you do a lot of work you run up a lot of costs. The test is whether
>> the benefits acrued outway the costs incurred. We will have to wait
>> for history to find out.
>
>Normally financially rational people do a business case first.

The outcomes are definitely worth the costs.
Whether we get the outcomes or not we will have to wait for history to
tell us.

DM
personal opinion only
---------------------
APR - http://politics.sunnybar...