How to capitalize a number of characters in a word

Hi, there

For example, how can i get “HelLoha” from “helloha”

Looking forward to hearing from you. Thanks in advance

For example, how can i get “HelLoha” from “helloha”

Probably one hundred million ways. One could be this:

x = “helloha” # => “helloha”
x.capitalize! # => “Helloha”
x[2,1] = x[2,1].capitalize # => “L”

x # => “HeLloha”

And yeah instead of 2,1 you would do 3,1.

On Tue, Jul 22, 2008 at 11:10 PM, Cheyne Li
[email protected] wrote:

For example, how can i get “HelLoha” from “helloha”

Can you explain why those are precisely the ones to capitalize?

On Tue, Jul 22, 2008 at 9:10 PM, Cheyne Li
[email protected] wrote:

Hi, there

For example, how can i get “HelLoha” from “helloha”

Here’s one for fun…

SEPARATION, s = 32, ‘helloha’
(my_letters = [0, 3]).each {|b| s[b] -= SEPARATION}
puts s

…it’s goofy, because I’m golfing and also trying to be descriptive
at the same time :slight_smile:

Of course, I’m making a large assumption that the letters to be
capitalized are based on position.

Here’s a contrived one…

include ‘matrix’
(Vector[‘helloha’.unpack('C’)] - Vector[1,0,0,1,0,0,0] *
32).to_a.pack(‘C*’)

…umm, ugly.

Todd

Cheyne Li wrote:

Hi, there

For example, how can i get “HelLoha” from “helloha”

Looking forward to hearing from you. Thanks in advance

Hi Li,

irb(main):016:0> a=“HelLoha”
=> “HelLoha”
irb(main):017:0> a.downcase
=> “helloha”
irb(main):018:0>

Regards,
P.Raveendran

Raveendran J. wrote:

Cheyne Li wrote:

Hi, there

For example, how can i get “HelLoha” from “helloha”

Looking forward to hearing from you. Thanks in advance

Hi Li,

irb(main):016:0> a=“HelLoha”
=> “HelLoha”
irb(main):017:0> a.downcase
=> “helloha”
irb(main):018:0>

So simply reversible

irb(main):016:0> a=“HelLoha”
=> “HelLoha”
irb(main):017:0> a.downcase
=> “helloha”
irb(main):018:0> a=“helloha”
=> “helloha”
irb(main):019:0> a=a.capitalize!
=> “Helloha”
irb(main):020:0> a=a[0,3]+a[3,1].capitalize!+a[4,3]
=> “HelLoha”
irb(main):021:0>

Regards,
P.Raveendran
http://raveendran.wordpress.com

On Tue, Jul 22, 2008 at 11:18 PM, Raveendran J.
[email protected] wrote:

=> “HelLoha”
irb(main):017:0> a.downcase
=> “helloha”
irb(main):018:0>

Regards,
P.Raveendran

There must be a language barrier. I thought the point was to turn
‘helloha’ into ‘HelLoha’, and not the reverse.

Todd

On Tue, Jul 22, 2008 at 11:54 PM, Raveendran J.
[email protected] wrote:

Yes. mistake from my side sorry for the inconvenience.

Well, you might be right with your first post, since you never know
what the original poster meant. Not everyone, after all, has a
command of the english language. And that would include me, lol.

Todd

Todd B. wrote:

On Tue, Jul 22, 2008 at 11:18 PM, Raveendran J.
[email protected] wrote:

=> “HelLoha”
irb(main):017:0> a.downcase
=> “helloha”
irb(main):018:0>

Regards,
P.Raveendran

There must be a language barrier. I thought the point was to turn
‘helloha’ into ‘HelLoha’, and not the reverse.

Todd

Thank you~

Todd B. wrote:

On Tue, Jul 22, 2008 at 11:18 PM, Raveendran J.
[email protected] wrote:

=> “HelLoha”
irb(main):017:0> a.downcase
=> “helloha”
irb(main):018:0>

Regards,
P.Raveendran

There must be a language barrier. I thought the point was to turn
‘helloha’ into ‘HelLoha’, and not the reverse.

Todd

Hi All(Todd),

Yes. mistake from my side sorry for the inconvenience.

Regards,
P.Raveendran

class String
def capitalize_at(*indices)
str = self.dup
indices.each do |i|
if chr = self[i, 1]
str[i,1] = chr.upcase
else
raise IndexError.new(“character #{i} out of index”)
end
end
str
end
end

“helloha”.capitalize_at(0, 3) # => “HelLoha”

If you find yourself doing it frequently.

There must be a language barrier.

Isn’t it, after all, the “natural” languages which separate us all
mostly, while Ruby unites our thinking brains … :slight_smile:

Cheyne Li, Ruben’s example is probably the best if you think you need
to do this more often. Ruby by default has .capitalize already, so I’d
guess a .capitalize_at method seems like a good name.

Marc H. wrote:

For example, how can i get “HelLoha” from “helloha”

Probably one hundred million ways. One could be this:

x = “helloha” # => “helloha”
x.capitalize! # => “Helloha”
x[2,1] = x[2,1].capitalize # => “L”

x # => “HeLloha”

And yeah instead of 2,1 you would do 3,1.

Thank you very much. It helps a lot

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs