Format string

Hello,

In other language there is often a function for format a string.
Format(“Hello %s”,[“Stonebreaker”])

I’m would like to use this for localization strings so the “#{var}”
would not be a good chooise.

Is there a already built in function for this?

Kind regards
Jon

Jon S. wrote:

In other language there is often a function for format a string.
Format(“Hello %s”,[“Stonebreaker”])

I’m would like to use this for localization strings so the “#{var}”
would not be a good chooise.

I’m a bit curious why the #{} wouldn’t fit your needs, since you can put
just about any method return within the curly brackets. sprintf and such
are just needed when you need strict formatting, and your example
doesn’t.

For example:
i = 1
puts “Text is #{[“Test 1”, “Test 2”][i]}”

Produces: “Text is Test 2”

Best regards,

Jari W.

On Nov 12, 6:29 am, Jon S. [email protected] wrote:

In other language there is often a function for format a string.
Format(“Hello %s”,[“Stonebreaker”])

I’m would like to use this for localization strings so the “#{var}”
would not be a good chooise.

Is there a already built in function for this?

Yes. RTFriendlyM:

phrogz$ ri String#%
--------------------------------------------------------------- String#
%
str % arg => new_str

 Format---Uses str as a format specification, and returns the
 result of applying it to arg. If the format specification

contains
more than one substitution, then arg must be an Array containing
the values to be substituted. See Kernel::sprintf for details of
the format string.

    "%05d" % 123                       #=> "00123"
    "%-5s: %08x" % [ "ID", self.id ]   #=> "ID   : 200e14d6"

phrogz$ ri sprintf

Kernel#sprintf
format(format_string [, arguments…] ) => string
sprintf(format_string [, arguments…] ) => string

 Returns the string resulting from applying format_string to any
 additional arguments. Within the format string, any characters
 other than format sequences are copied to the result. A format
 sequence consists of a percent sign, followed by optional flags,
 width, and precision indicators, then terminated with a field

type
character. The field type controls how the corresponding sprintf
argument is to be interpreted, while the flags modify that
interpretation. The field type characters are listed in the
table
at the end of this section. The flag characters are:

   Flag     | Applies to   | Meaning
   ---------+--------------

±----------------------------------------
space | bdeEfgGiouxX | Leave a space at the start of
| | positive numbers.
---------±-------------
±----------------------------------------
(digit)$ | all | Specifies the absolute argument
number
| | for this field. Absolute and relative
| | argument numbers cannot be mixed in a
| | sprintf string.
---------±-------------
±----------------------------------------
# | beEfgGoxX | Use an alternative format. For the
| | conversions o', x’, X', and b’,
| | prefix the result with 0'', 0x’‘,
0X'', | | and 0b’‘, respectively. For e', | | E’, f', g’, and ‘G’, force a
decimal
| | point to be added, even if no digits
follow.
| | For `g’ and ‘G’, do not remove
trailing zeros.
---------±-------------
±----------------------------------------
+ | bdeEfgGiouxX | Add a leading plus sign to positive
numbers.
---------±-------------
±----------------------------------------
- | all | Left-justify the result of this
conversion.
---------±-------------
±----------------------------------------
0 (zero) | bdeEfgGiouxX | Pad with zeros, not spaces.
---------±-------------
±----------------------------------------
* | all | Use the next argument as the field
width.
| | If negative, left-justify the result.
If the
| | asterisk is followed by a number and
a dollar
| | sign, use the indicated argument as
the width.

 The field width is an optional integer, followed optionally by a
 period and a precision. The width specifies the minimum number

of
characters that will be written to the result for this field.
For
numeric fields, the precision controls the number of decimal
places displayed. For string fields, the precision determines
the
maximum number of characters to be copied from the string.
(Thus,
the format sequence %10.10s will always contribute exactly ten
characters to the result.)

 The field types are:

     Field |  Conversion
     ------

±-------------------------------------------------------------
b | Convert argument as a binary number.
c | Argument is the numeric code for a single character.
d | Convert argument as a decimal number.
E | Equivalent to e', but uses an uppercase E to indicate | the exponent. e | Convert floating point argument into exponential notation | with one digit before the decimal point. The precision | determines the number of fractional digits (defaulting to six). f | Convert floating point argument as [-]ddd.ddd, | where the precision determines the number of digits after | the decimal point. G | Equivalent to g’, but use an uppercase E' in exponent form. g | Convert a floating point number using exponential form | if the exponent is less than -4 or greater than or | equal to the precision, or in d.dddd form otherwise. i | Identical to d’.
o | Convert argument as an octal number.
p | The valuing of argument.inspect.
s | Argument is a string to be substituted. If the format
| sequence contains a precision, at most that many
characters
| will be copied.
u | Treat argument as an unsigned decimal number.
Negative integers
| are displayed as a 32 bit two’s complement plus one
for the
| underlying architecture; that is, 2 ** 32 + n.
However, since
| Ruby has no inherent limit on bits used to represent
the
| integer, this value is preceded by two dots (…) in
order to
| indicate a infinite number of leading sign bits.
X | Convert argument as a hexadecimal number using
uppercase
| letters. Negative numbers will be displayed with two
| leading periods (representing an infinite string of
| leading 'FF’s.
x | Convert argument as a hexadecimal number.
| Negative numbers will be displayed with two
| leading periods (representing an infinite string of
| leading 'ff’s.

 Examples:

    sprintf("%d %04x", 123, 123)               #=> "123 007b"
    sprintf("%08b '%4s'", 123, 123)            #=> "01111011 '

123’"
sprintf(“%1$*2$s %2$d %1$s”, “hello”, 8) #=> " hello 8
hello"
sprintf(“%1$*2$s %2$d”, “hello”, -8) #=> “hello -8”
sprintf(“%+g:% g:%-g”, 1.23, 1.23, 1.23) #=> “+1.23:
1.23:1.23”
sprintf(“%u”, -123) #=> “…4294967173”

Jon S. wrote:

I’m would like to use this for localization strings

What does that mean?

In other language there is often a function for format a string.
Format(“Hello %s”,[“Stonebreaker”])

str = “Hello %s” % “Stonebreaker”
puts str

–output:–
Hello Stonebreaker

#-------------

names = [“Stonebreaker”, “world”]

for name in names:
puts “Hello %s” % name
end

–output:–
Hello Stonebreaker
Hello world

#---------------

names = [“Stonebreaker”, “world”]

for name in names:
puts “Hello #{name}”
end

–output:–
Hello Stonebreaker
Hello world