I just started learning Ruby this weekend, and I have to say that I’ve
fallen in love with it so far. It’s a beautiful beautiful language. =0)
In messing around with it, I found that I needed a method that capitalizes
all the words in a string, assuming that each word is seperated by a space.
I did this, and it works fine:
class String
def capwords
self.split.each { |word| word.capitalize! }.join(" ")
end
end
It does what I need, but it got me thinking. How would I make this
destructive? In other words, rather than having to do an assingment like
this:
title = “This is the title”
title = title.capwords
I’d like to do this instesad:
title.capwords!
I tried creating a capwords! method, using the line from capwords, and I
added a “self =” directive in front of the code of the capwords method, but
Ruby tells me “Can’t change the value of self”.
Would anyone have any words of wisdom, or at least a directional pointer, on
what to do with this? I’d really appreciate it!
I just started learning Ruby this weekend, and I have to say that I’ve
fallen in love with it so far. It’s a beautiful beautiful language. =0)
It only gets better as you learn and use it
In messing around with it, I found that I needed a method that capitalizes
all the words in a string, assuming that each word is seperated by a space.
I did this, and it works fine:
class String
def capwords
self.split.each { |word| word.capitalize! }.join(" ")
end
end
It does what I need, but it got me thinking. How would I make this
destructive? In other words, rather than having to do an assingment like
this:
title = “This is the title”
title = title.capwords
I’d like to do this instesad:
title.capwords!
You gotta use destructive methods to write a destrucive method. Not
fully tested…
irb(main):001:0> string = “this is the string”
“this is the string”
irb(main):002:0> class String; def capwords!; gsub!(/\b[a-z]/) {|ltr|
ltr.upcase }; end; end
nil
irb(main):003:0> string.capwords!
“This Is The String”
class String
def capwords!
replace split.collect { |word| word.capitalize }.join(" ")
end
end
Mind that you loose information about original whitespaces, all of them
are replaced with a single space character.
True. I didn’t think about that, although it [luckily] didn’t matter for my
use. Thanks for pointing out ‘replace’. Despite having the reference and
the pickaxe right in front of me, I missed that one.
I found that I needed a method that
capitalizes all the words in a string, assuming that each word is
seperated by a space. I did this, and it works fine:
class String
def capwords
self.split.each { |word| word.capitalize! }.join(" ")
end
end
It does what I need, but it got me thinking. How would I make this
destructive?
I’d like to do this instesad:
title.capwords!
You gotta use destructive methods to write a destrucive method. Not
fully tested…
irb(main):001:0> string = “this is the string”
“this is the string”
irb(main):002:0> class String; def capwords!; gsub!(/\b[a-z]/) {|ltr|
ltr.upcase }; end; end
nil
irb(main):003:0> string.capwords!
“This Is The String”
Thank you for this! This solves the problem that Gennady spoke of with the
whitespace, which is very good indeed.
all the words in a string, assuming that each word is seperated by a
space. I did this, and it works fine:
class String
def capwords
self.split.each { |word| word.capitalize! }.join(" ")
end
end
It does what I need, but it got me thinking. How would I make this
destructive? In other words, rather than having to do an assingment like
this:
class String
def capwords!
self.gsub!(/[1]/) {|x| x.upcase }
end
end
Oops, now that I read over your reply, I realize that I wasn’t very clear on
what I wanted to accomplish. I apologize. I wanted to convert “This s a
title” to “This Is A Title”. The “\b” that Joel used in his demonstration
did what I was looking for.
I thank you for your reply.
Jeremy
···
In message “How to add a destructive method to the String class…” > on 03/03/12, Jeremy thinker5555@yahoo.com writes: