Prepend a character to a string in ruby

Hi,

I am trying to insert a character to a string at the beginning of a
string. How is it possible in ruby.
I tried using
s.times do |s|
@space_availability+=“0”
end
But this would add at the end of the string.
The @space_availability is a string with 1’s and 0’s. I need to add 0’s
at the beginning of this string.
I am a newbie in ruby

On Wednesday 31 August 2011 20:00:40 ruby rails wrote:

at the beginning of this string.
I am a newbie in ruby

@space_availlability.insert 0, “0”

You can find documentation for a class or method using the ri command
line
tool:

ri String

or

ri String#insert

I hope this helps

Stefano

Another way:

x = “foobar” # => “foobar”
x[0,0] = "this is prepended " # => "this is prepended "
x # => “this is prepended foobar”

x variable now is “this is prepended foobar” as you can see from IRB
output.

There are many more ways. At least one with a Regex, but it is often
said that Regex has an overhead and hence a speed penalty, thus I think
it is usually better to use a Regex only when needed.

There is also another way in where you re-assemble a string.

x = “foobar”

x = "abcdef " + x

But this would create a new string object.

If you use << then you do not create a new string object. (But this
means append to… sadly there is no prepend to … sometimes I wanted
to use >> to mean prepend…)

The other answers are better. I am just posting this for yet another
way to prepend text to a string: String interpolation.

question = "Nancy Francis: Back here live at the Waterfront Village 

with my friend
the zombie, Jonathon. You’re looking good, Jonathon. Jonathon just
got an awesome face paint job. What do you think?\n\n"

answer = "Jonathan: I like turtles!"

dialogue = "#{question}#{answer}"

On Wed, Aug 31, 2011 at 1:00 PM, ruby rails [email protected]
wrote:

I am trying to insert a character to a string at the beginning of a
string. How is it possible in ruby.
I tried using
s.times do |s|
@space_availability+=“0”
end
But this would add at the end of the string.
The @space_availability is a string with 1’s and 0’s. I need to add 0’s
at the beginning of this string.
I am a newbie in ruby

You can use String#rjust

irb(main):023:0> @space_availability = ‘x’
=> “x”
irb(main):024:0> @space_availability = @space_availability.rjust 5, ‘0’
=> “0000x”

However, chances are that you want printf, sprintf or % operator. For
example

irb(main):013:0> sprintf ‘%0*d’, 10, 8
=> “0000000008”

So, for numbers

@space_availability = sprintf ‘%0*d’, s, int_value

If you want the string to represent a binary number you can do

irb(main):015:0> sprintf ‘%0*b’, 8, 123
=> “01111011”

Note, the ‘’ in the format string indicates there is an argument
indicating the length before the value. If the length is fixed you
can replace '
’ with the length:

irb(main):016:0> sprintf ‘%08b’, 8, 123
=> “00001000”

Kind regards

robert

On Wed, Aug 31, 2011 at 7:19 AM, Michal S. [email protected]
wrote:

It is sad there is no obvious method for that.

But if it makes you happy, there can be :slight_smile:

ruby-1.8.7-p352 > class String
ruby-1.8.7-p352 ?> def >>(arg)
ruby-1.8.7-p352 ?> self.insert(0, arg)
ruby-1.8.7-p352 ?> end
ruby-1.8.7-p352 ?> end
=> nil
ruby-1.8.7-p352 > string = “bar”
=> “bar”
ruby-1.8.7-p352 > string >> “foo”
=> “foobar”
ruby-1.8.7-p352 >

On 31 August 2011 13:44, Marc H. [email protected] wrote:

Another way:

x = “foobar” # => “foobar”
x[0,0] = "this is prepended " # => "this is prepended "
x # => “this is prepended foobar”

x variable now is “this is prepended foobar” as you can see from IRB
output.

This is nice.

It is sad there is no obvious method for that.

Thanks

Michal

On Aug 31, 2011, at 08:29 AM, Hassan S.
[email protected] wrote:
On Wed, Aug 31, 2011 at 7:19 AM, Michal S. [email protected]
wrote:

It is sad there is no obvious method for that.

But if it makes you happy, there can be :slight_smile:

ruby-1.8.7-p352 > class String
ruby-1.8.7-p352 ?> def >>(arg)
ruby-1.8.7-p352 ?> self.insert(0, arg)
ruby-1.8.7-p352 ?> end
ruby-1.8.7-p352 ?> end
=> nil
ruby-1.8.7-p352 > string = “bar”
=> “bar”
ruby-1.8.7-p352 > string >> “foo”
=> “foobar”
ruby-1.8.7-p352 >

Since we’re monkeypatching and mutating anyway, I like reversing the
order so that the order of the arguments is the same as in the result:

irb(main):001:0> class String
irb(main):002:1> def >>(other)
irb(main):003:2> other.insert(0,self)
irb(main):004:2> end
irb(main):005:1> end
=> nil
irb(main):006:0> str = “bar”
=> “bar”
irb(main):007:0> “foo” >> str
=> “foobar”

On Wed, Aug 31, 2011 at 5:00 AM, ruby rails
[email protected]wrote:

at the beginning of this string.
I am a newbie in ruby

It is coming:

String#prepend has been accepted as revision r29120.

On Wed, Aug 31, 2011 at 4:46 PM, Gavin K. [email protected] wrote:

ruby-1.8.7-p352 > class String

=> “bar”
irb(main):007:0> “foo” >> str
=> “foobar”

That triggered another idea:

irb(main):001:0> class String
irb(main):002:1> def head=(s)
irb(main):003:2> insert 0, s
irb(main):004:2> end
irb(main):005:1> def tail=(s)
irb(main):006:2> self << s
irb(main):007:2> end
irb(main):008:1> end
=> nil
irb(main):009:0> s = “foo”
=> “foo”
irb(main):010:0> s.head = “begin(”
=> “begin(”
irb(main):011:0> s
=> “begin(foo”
irb(main):012:0> s.tail = “)end”
=> “)end”
irb(main):013:0> s
=> “begin(foo)end”

Note the absence of readers. Reasoning about why left as exercise for
the reader. :slight_smile:

Kind regards

robert