Ruby string

string = "this is a String"
string.capitalize = "This is a string"
That works but changes my capital "a String" to lowercase.

I only want to capitalize first letter.
So I try"
string[0..0].capitalize
irb => "T"
OK
string[0..0].capitalize!
irb => "T"
irb string
irb => "this is a string"

I am a noob but I don't get that.
Where is my capitalised first letter.

Anybody?

···

--
Posted via http://www.ruby-forum.com/.

Peter wrote:

string[0..0].capitalize!

string[0, 1] = string[0, 1].capitalize

···

--
Posted via http://www.ruby-forum.com/\.

Hi --

···

On Sat, 8 Apr 2006, Peter wrote:

string = "this is a String"
string.capitalize = "This is a string"
That works but changes my capital "a String" to lowercase.

I only want to capitalize first letter.
So I try"
string[0..0].capitalize
irb => "T"
OK
string[0..0].capitalize!
irb => "T"
irb string
irb => "this is a string"

I am a noob but I don't get that.
Where is my capitalised first letter.

What you've done is to create a new string, consisting of the single
character 't'. Then you've done an in-place capitalize on that
string; then you've discarded it :slight_smile:

I can't think of a better way to do this than what's already been
posted, though it really does feel like there should be.

David

--
David A. Black (dblack@wobblini.net)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" coming in PDF April 15, and in paper May 1!

Alex Barrett wrote:

Peter wrote:

string[0..0].capitalize!

string[0, 1] = string[0, 1].capitalize

Thanx

···

--
Posted via http://www.ruby-forum.com/\.

string = "this is a String"
string.capitalize = "This is a string"
That works but changes my capital "a String" to lowercase.

I only want to capitalize first letter.
So I try"
string[0..0].capitalize
irb => "T"
OK
string[0..0].capitalize!
irb => "T"
irb string
irb => "this is a string"

What you've done is to create a new string, consisting of the single
character 't'. Then you've done an in-place capitalize on that
string; then you've discarded it :slight_smile:

Another not-so-brilliant way to do is to put the created string back to the original string by doing this:

string[0..0] = string[0..0].upcase

I can't think of a better way to do this than what's already been
posted, though it really does feel like there should be.

I agree.

···

On Sat, 8 Apr 2006, dblack@wobblini.net wrote:

On Sat, 8 Apr 2006, Peter wrote:

unknown wrote:

I can't think of a better way to do this than what's already been
posted, though it really does feel like there should be.

Actually, there is :S
You can save three whole keypresses (or just two if you dislike
whitespace) by doing:

···

string[0, 1] = string[0, 1].capitalize

--
Posted via http://www.ruby-forum.com/\.

Alex Barrett wrote:

string[0, 1] = string[0, 1].capitalize

Curses, trust me to repost the same thing. I meant:

string[0] = string[0, 1].capitalize

Must remember to check my pasting before submitting.

···

--
Posted via http://www.ruby-forum.com/\.

Hi --

···

On Sat, 8 Apr 2006, Alex Barrett wrote:

unknown wrote:

I can't think of a better way to do this than what's already been
posted, though it really does feel like there should be.

Actually, there is :S
You can save three whole keypresses (or just two if you dislike
whitespace) by doing:

string[0, 1] = string[0, 1].capitalize

Isn't that the same as what you'd already posted?

David

--
David A. Black (dblack@wobblini.net)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" coming in PDF April 15, and in paper May 1!

Hi --

···

On Sat, 8 Apr 2006, Alex Barrett wrote:

Alex Barrett wrote:

string[0, 1] = string[0, 1].capitalize

Curses, trust me to repost the same thing. I meant:

string[0] = string[0, 1].capitalize

Must remember to check my pasting before submitting.

And I should remember to read the rest of the thread before replying
:slight_smile:

David

--
David A. Black (dblack@wobblini.net)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" coming in PDF April 15, and in paper May 1!

I love Ruby but this is cumbersome.
There are plenty of situations where people want to
capitalize only the first letter.

By saying:
string[0,1].capitalize! with ! should read like:
Capitalize only the first character within string 'string'
Or is that stupid thinking.
Its a method on the first character.

Funny.
But thanks all.

···

--
Posted via http://www.ruby-forum.com/.

Peter wrote:

I love Ruby but this is cumbersome.

Well, i don't think this is a Ruby specific behavior. I have just tried
the same thing in Python:

s = "this is a String"
s.capitalize()

'This is a string'

i.e. it has absolutely the same semantics as in Ruby.

There are plenty of situations where people want to
capitalize only the first letter.

The problem is not the capitalization of the first letter (as this
works), but rather that it turns the other first-letters to lowercase.
I don't know why the capitalize() function is defined like this - both
in Python and Ruby...

By saying:
string[0,1].capitalize! with ! should read like:
Capitalize only the first character within string 'string'

What about this:
x = string[0,1]
x.capitalize!

Should this still capitalize only the first character within string
'string'? I think the answer is obviously no. But your code is
essentially the same, without using the temporary variable x.

bw,
Peter

Hi --

I love Ruby but this is cumbersome.
There are plenty of situations where people want to
capitalize only the first letter.

By saying:
string[0,1].capitalize! with ! should read like:
Capitalize only the first character within string 'string'
Or is that stupid thinking.
Its a method on the first character.

Funny.
But thanks all.

It may be cumbersome but the alternative you describe would be worse
:slight_smile:

Ruby is pretty strict about left-to-right evaluation. This:

   string[0,1]

means something: a one-character substring of string. What you do
*with* that substring -- what messages you send it -- is a completely
separate, subsequent transaction.

David

···

On Sat, 8 Apr 2006, Peter wrote:

--
David A. Black (dblack@wobblini.net)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" coming in PDF April 15, and in paper May 1!

Speaking of alternatives, here is how you also could do it:
string[0] = string[0].chr.upcase

···

2006/4/8, dblack@wobblini.net <dblack@wobblini.net>:

Hi --

On Sat, 8 Apr 2006, Peter wrote:

> I love Ruby but this is cumbersome.
> There are plenty of situations where people want to
> capitalize only the first letter.
>
> By saying:
> string[0,1].capitalize! with ! should read like:
> Capitalize only the first character within string 'string'
> Or is that stupid thinking.
> Its a method on the first character.
>
> Funny.
> But thanks all.

It may be cumbersome but the alternative you describe would be worse
:slight_smile:

Ruby is pretty strict about left-to-right evaluation. This:

   string[0,1]

means something: a one-character substring of string. What you do
*with* that substring -- what messages you send it -- is a completely
separate, subsequent transaction.

David

--
David A. Black (dblack@wobblini.net)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" coming in PDF April 15, and in paper May 1!
Ruby for Rails

--
"winners never quit, quitters never win"

Jeppe Jakobsen wrote:

string[0] = string[0].chr.upcase

Yep this is the one.
I think [0] is more natural.
I don't need a range.

So with a slight change:
string = "this is a String"
string[0] = string[0].chr.capitalize

Thanx All

···

--
Posted via http://www.ruby-forum.com/\.

Jeppe Jakobsen wrote:
> string[0] = string[0].chr.upcase

Yep this is the one.
I think [0] is more natural.
I don't need a range.

So with a slight change:
string = "this is a String"
string[0] = string[0].chr.capitalize

Thanx All

--
Posted via http://www.ruby-forum.com/\.

I am glad you like it, but why?

···

On 4/9/06, Peter <peter@negerzoen.be> wrote:
--
Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui
concerne l'univers, je n'en ai pas acquis la certitude absolue.

- Albert Einstein