Forum: Ruby Ruby string

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Peter (Guest)
on 2006-04-08 16:25
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?
Alex Barrett (Guest)
on 2006-04-08 16:45
Peter wrote:
> string[0..0].capitalize!

string[0, 1] = string[0, 1].capitalize
Peter (Guest)
on 2006-04-08 17:02
Alex Barrett wrote:
> Peter wrote:
>> string[0..0].capitalize!
>
> string[0, 1] = string[0, 1].capitalize

Thanx
unknown (Guest)
on 2006-04-08 17:07
(Received via mailing list)
Hi --

On Sat, 8 Apr 2006, Peter wrote:

> 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 :-)

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 (removed_email_address@domain.invalid)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" coming in PDF April 15, and in paper May 1!
http://www.manning.com/black
Alex Barrett (Guest)
on 2006-04-08 17:33
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
Alex Barrett (Guest)
on 2006-04-08 17:35
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.
Craig K. (Guest)
on 2006-04-08 17:35
(Received via mailing list)
On Sat, 8 Apr 2006, removed_email_address@domain.invalid wrote:

>> 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 :-)
>
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.
unknown (Guest)
on 2006-04-08 18:18
(Received via mailing list)
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 (removed_email_address@domain.invalid)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" coming in PDF April 15, and in paper May 1!
http://www.manning.com/black
unknown (Guest)
on 2006-04-08 18:18
(Received via mailing list)
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
:-)


David

--
David A. Black (removed_email_address@domain.invalid)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" coming in PDF April 15, and in paper May 1!
http://www.manning.com/black
Peter (Guest)
on 2006-04-08 18:59
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.
Peter S. (Guest)
on 2006-04-08 19:40
(Received via mailing list)
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
unknown (Guest)
on 2006-04-08 19:50
(Received via mailing list)
Hi --

On Sat, 8 Apr 2006, Peter wrote:

> Funny.
> But thanks all.

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

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 (removed_email_address@domain.invalid)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" coming in PDF April 15, and in paper May 1!
http://www.manning.com/black
Jeppe J. (Guest)
on 2006-04-09 01:20
(Received via mailing list)
Speaking of alternatives, here is how you also could do it:
string[0] = string[0].chr.upcase

2006/4/8, removed_email_address@domain.invalid 
<removed_email_address@domain.invalid>:
Peter (Guest)
on 2006-04-09 05:10
Jeppe J. 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
Robert D. (Guest)
on 2006-04-09 14:36
(Received via mailing list)
On 4/9/06, Peter <removed_email_address@domain.invalid> wrote:
> string[0] = string[0].chr.capitalize
>
> Thanx All
>
> --
> Posted via http://www.ruby-forum.com/.
>
> I am glad you like it, but why?


--
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
This topic is locked and can not be replied to.