Character Code

Hi:

My apologies if this is a ridiculously simple question…

Giiven the following code:

 line = file.readline.strip
 # line = '; some line'

 if line[0].to_s == ';'  # false! line[0] = 59, not ';'
     # ...
 end

My problem is, line[0] returns a fixnum, which doesn’t compare with the
’;’ string. So… how do perform this comparison?

Thanks,
Cameron

···


"Perhaps a lunatic was simply a minority of one"
George Orwell

line[0] == ?;

or

/^;/ =~ line

?;' is a Fixnum literal, represents ascii code of char ;'.

···

At Sat, 20 Jul 2002 04:21:08 +0900, the Bare wrote:

My problem is, line[0] returns a fixnum, which doesn’t compare with the
‘;’ string. So… how do perform this comparison?

line[0,1]

although i sometimes get errors with that, so

line[0…0]

seems to work best

line[0] just returns the bytecode. go figure.

~transami

···

On Fri, 2002-07-19 at 13:21, the Bare wrote:

Hi:

My apologies if this is a ridiculously simple question…

Giiven the following code:

 line = file.readline.strip
 # line = '; some line'

 if line[0].to_s == ';'  # false! line[0] = 59, not ';'
     # ...
 end

My problem is, line[0] returns a fixnum, which doesn’t compare with the
‘;’ string. So… how do perform this comparison?

Thanks,
Cameron


“Perhaps a lunatic was simply a minority of one”
George Orwell


~transami

(") dobee dobee do…
\v/
^ ^

Or: line[0…0] will give you a string.

Hal

···

----- Original Message -----
From: “GOTO Kentaro” gotoken@notwork.org
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Friday, July 19, 2002 2:28 PM
Subject: Re: Character Code

At Sat, 20 Jul 2002 04:21:08 +0900, > the Bare wrote:

My problem is, line[0] returns a fixnum, which doesn’t compare with the
‘;’ string. So… how do perform this comparison?

line[0] == ?;

or

/^;/ =~ line

?;' is a Fixnum literal, represents ascii code of char ;'.

?x being a shortcut for Fixnum literal is cool, but what an
incredible opportunity for code obfuscation! :slight_smile:

This is legitimate Ruby: “?;??;:?;”

irb(main):001:0> ?;??;:?;
59

Heh.

– Dossy

···

On 2002.07.20, GOTO Kentaro gotoken@notwork.org wrote:

?;' is a Fixnum literal, represents ascii code of char ;'.


Dossy Shiobara mail: dossy@panoptic.com
Panoptic Computer Network web: http://www.panoptic.com/
“He realized the fastest way to change is to laugh at your own
folly – then you can let go and quickly move on.” (p. 70)

Working in the other direction from everyone’s suggestions, I
might try:

if line[0].chr == ';'
    # ...
end

– Dossy

···


Dossy Shiobara mail: dossy@panoptic.com
Panoptic Computer Network web: http://www.panoptic.com/
“He realized the fastest way to change is to laugh at your own
folly – then you can let go and quickly move on.” (p. 70)

Dossy wrote:

Working in the other direction from everyone’s suggestions, I
might try:

if line[0].chr == ';'
    # ...
end

– Dossy

This has got to be the friendliest programming newsgroup in existence.
Thanks for everyone’s responses!

Cameron

Dossy, that’s probably the best one --LNO, least number of objects.

···

On Fri, 2002-07-19 at 17:46, Dossy wrote:

if line[0].chr == ';'
    # ...
end

– Dossy

~transami

(") dobee dobee do…
\v/
^ ^

Hi,

···

In message “Re: Character Code” on 02/07/20, Dossy dossy@panoptic.com writes:

?x being a shortcut for Fixnum literal is cool, but what an
incredible opportunity for code obfuscation! :slight_smile:

Yeah, I had to leave place for code obfuscation opportunities for the
future contest challengers. :wink:

						matz.

yeah…we’re not jaded :slight_smile:

sorry for the pun.

todd

···

On Sat, Jul 20, 2002 at 11:21:37AM +0900, the Bare wrote:

Dossy wrote:

Working in the other direction from everyone’s suggestions, I
might try:

if line[0].chr == ‘;’
# …
end

– Dossy

This has got to be the friendliest programming newsgroup in existence.
Thanks for everyone’s responses!

Cameron


[It] contains “vegetable stabilizer” which sounds ominous. How unstable are vegetables?
Jeff Zahn