Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to convert character code to a String?

1 view
Skip to first unread message

Ryo Furue

unread,
Jan 6, 2002, 9:19:19 AM1/6/02
to
Hi there,

I fear I might be missing something obvious, but I'm wondering how to
get a one-character String with a given character code. By examining
the pickaxe book and the archive of this mailing list, I've found this
so far:

c = 123
s = String.new
s << c
list_of_strings.push(s)

This is a bit verbose, and I hope I can do something like the
following instead of the above:

c = 123
list_of_strings.push(char(c))

It's relatively easy to obtain character codes (String#[]), so I
expect there's a straight forward way to obtain a String from a
character code. If there's no built-in way, that's OK, and I'll
define a method like "char" above or add an method to the Fixnum
class.

Thanks,
Ryo

Kent Dahl

unread,
Jan 6, 2002, 3:40:03 PM1/6/02
to
Ryo Furue wrote:
> I fear I might be missing something obvious, but I'm wondering how to
> get a one-character String with a given character code.

Integer#chr is what you need:


------------------------------------------------------------ Integer#chr
int.chr -> aString
------------------------------------------------------------------------
Returns a string containing the ASCII character represented by the
receiver's value.
65.chr #=> "A"
?a.chr #=> "a"
230.chr #=> "\346"

--
<[ Kent Dahl ]>================<[ http://www.stud.ntnu.no/~kentda/ ]>
)____(stud.techn.;ind.øk.data)||(softwareDeveloper.at(Trustix))_(
/"Opinions expressed are mine and not those of my Employer, "\
( "the University, my girlfriend, stray cats, banana fruitflies, " )
\"nor the frontal lobe of my left cerebral hemisphere. "/

Yukihiro Matsumoto

unread,
Jan 6, 2002, 7:13:12 PM1/6/02
to
Hi,

In message "[ruby-talk:30347] How to convert character code to a String?"


on 02/01/06, Ryo Furue <furu...@ccsr.u-tokyo.ac.jp> writes:

|expect there's a straight forward way to obtain a String from a
|character code. If there's no built-in way, that's OK, and I'll
|define a method like "char" above or add an method to the Fixnum
|class.

Fixnum#chr

matz.

Mark Hahn

unread,
Jan 6, 2002, 9:20:50 PM1/6/02
to

I cannot get the connect method in fxruby to work. The fxruby manual says:

------------ begin quote from manual ----------
.. This new model was introduced with the FXRuby-0.99.179 release ...
Event Model
FXRuby implements a new, simplified approach to this built on top of the old
model. It more or less mimics the syntax used in Ruby/GTK; you can attach a
message handler block to a widget using a new connect instance method, e.g.

aButton = FXButton.new(parent, "Push Me")
aButton.connect(SEL_COMMAND) { |sender, sel, ptr|
puts "Ouch!"
}
..
------------ end quote from manual ----------

I am running FXRuby-0.99.181-win32.exe with a cygwin Ruby. The following
code is a combination of the above snippet with the "hello world" snippet in
the same manual:

require 'fox'
require 'fox/responder'
include Fox
include Responder

app = FXApp.new("Hello", "FoxTest")
app.init(ARGV)
main = FXMainWindow.new(app, "Hello", nil, nil, DECOR_ALL)
FXButton.new(main, "Push Me").connect(SEL_COMMAND) { puts "Ouch!" }
app.create
main.show(PLACEMENT_SCREEN)
app.run

This code gives the error: "undefined method `connect' for Push
Me:Fox::FXButton (NameError)". If I remove the .connect call and block, the
button appears properly in the window.

Help please?

Ryo Furue

unread,
Jan 7, 2002, 8:48:07 AM1/7/02
to
ma...@ruby-lang.org (Yukihiro Matsumoto) wrote in message news:<1010361965.56292...@ev.netlab.jp>...

Thank you, Kent and Matz! Yes, I was missing something
(semi-)obvious.

As Kent said, it's Integer#chr. Of course, Integer is a superclass of
Fixnum.... Just out of curiosity, this makes me wonder why it isn't
Fixnum#chr. Is that because you envision a future where Ruby handles
four-byte characters (assuming a Fixnum is 31 bits or longer)?

Finally, a feeble excuse :-) I didn't find #chr because I checked
Fixnum but not Integer. This is the reason why I said
"(semi-)obvious".

BTW, I bought a copy of "Ruby in a Nutshell" today. The book is a
nice handy reference. I'm happy with it.

Thanks again,
Ryo

Lyle Johnson

unread,
Jan 7, 2002, 10:36:22 AM1/7/02
to
> This code gives the error: "undefined method `connect' for Push
> Me:Fox::FXButton (NameError)". If I remove the .connect call and block,
the
> button appears properly in the window.

Is there any chance that you installed the new version of FXRuby (i.e.
version 0.99.181) to some directory other than the Ruby installation
directory, and that you're actually referencing an older version of FXRuby
somewhere else on the disk? I just blew away my old copy of FXRuby, i.e. the
files:

C:\ruby\lib\ruby\site_ruby\1.6\i386-cygwin\fox.so
C:\ruby\lib\ruby\site_ruby\1.6\fox\*.rb

and then reinstalled FXRuby-0.99.181-win32.exe and your example worked as
expected.


0 new messages