Formated strings

Hi,

How can I write a formated text to a string (like I would do with printf

when using an IO object)? I want to append a 16 character decimal to an
existing string, with printf I would do that with ("%16d",decimal).

Thanks,
Maurício

Hi–

···

On Wed, 14 Aug 2002, Maurício wrote:

Hi,

How can I write a formated text to a string (like I would do with printf

when using an IO object)? I want to append a 16 character decimal to an
existing string, with printf I would do that with ("%16d",decimal).

With sprintf.

David


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

existing_string += sprintf("%16d", decimal)

···

On Wed, Aug 14, 2002 at 05:40:22AM +0900, Maur?cio wrote:

Hi,

How can I write a formated text to a string (like I would do with printf

when using an IO object)? I want to append a 16 character decimal to an
existing string, with printf I would do that with ("%16d",decimal).

Thanks,
Maurício


Alan Chen
Digikata LLC

Maurício wrote:

Hi,

How can I write a formated text to a string (like I would do with printf

when using an IO object)? I want to append a 16 character decimal to an
existing string, with printf I would do that with ("%16d",decimal).

Thanks,
Maurício

If s is a string:

s << “%16” % decimal

(…)
existing_string += sprintf("%16d", decimal)


Alan Chen
Digikata LLC
http://digikata.com

I'm curious, since I'm new to Ruby: how does sprintf is called, i.e., is

it a method of something or it’s some kind of global function (or sintatic
sugar for something)?

Maurício

It’s a private method of the Kernel module. As Object includes Kernel, the
default object has all the methods in Kernel, including sprintf, printf
and stuff:

irb(main):001:0> self
main
irb(main):002:0> self.kind_of? Kernel
true
irb(main):003:0> sprintf “%16d”, 23
" 23"

···

On Wed, Aug 14, 2002 at 06:23:39AM +0900, Maurício wrote:

(…)
existing_string += sprintf("%16d", decimal)


Alan Chen
Digikata LLC
http://digikata.com

I'm curious, since I'm new to Ruby: how does sprintf is called, i.e., is

it a method of something or it’s some kind of global function (or sintatic
sugar for something)?


_ _

__ __ | | ___ _ __ ___ __ _ _ __
’_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

<|ryan|> I don’t use deb
u poor man
netgod: heh
apt-get install task-p0rn

I believe that sprintf is provided via the Kernel module.

···

On Wed, Aug 14, 2002 at 06:23:39AM +0900, Maur?cio wrote:

existing_string += sprintf("%16d", decimal)

I'm curious, since I'm new to Ruby: how does sprintf is called, i.e., is

it a method of something or it’s some kind of global function (or sintatic
sugar for something)?

Maurício


Alan Chen
Digikata LLC