# Crunching Text Not Working in a Loop

**URL:** <https://www.ruby-forum.com/t/crunching-text-not-working-in-a-loop/193014>\
**Category:** Ruby\
**Created:** [August 10, 2010, 12:41am UTC](https://www.ruby-forum.com/t/crunching-text-not-working-in-a-loop/193014 "2010-08-10T00:41:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![richardfairhurst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c37758/32.png) [@richardfairhurst](https://www.ruby-forum.com/u/richardfairhurst)\
**Post date:** [August 10, 2010, 12:41am UTC](https://www.ruby-forum.com/t/crunching-text-not-working-in-a-loop/193014/1 "2010-08-10T00:41:24Z")

</div>

Greetings, folks!

I need to be able to capitalize words inside quote marks, as part of a  
string of words. I need to use proper publishing capitalization  
protocols which is more involved than simply making the first letter of  
each word uppercase. Thus the appended script is greatly truncated!

I would be very grateful if someone could please advise me on why the  
following doesnâ€™t work as part of a repeating loop or with typographic  
quote marks, but does work as a simple standalone script.

Blessings and thank you!

Richard F.

* * *

The following does work, but only if the first quote is a straight  
quote: ’ or ". It doesnâ€™t work with typesetter marks: â€˜ or â€œ

#!/usr/bin/env ruby

require ‘rubygems’  
require “jcode”; $KCODE = “UTF8”  
require ‘appscript’; include Appscript  
require ‘osax’; include OSAX

# copy the text: “abc”

item = osax.the\_clipboard()  
item1 = item[0,1]  
item2 = item[1…-1]  
item = item1 + item2.capitalize!  
osax.set\_the\_clipboard\_to(item) #=\> "Abc"s

* * *

BUT it doesnâ€™t work at all as part of a loop:

#!/usr/bin/env ruby

require ‘rubygems’  
require “jcode”; $KCODE = “UTF8”  
require ‘appscript’; include Appscript  
require ‘osax’; include OSAX

openingQuoteMark = ["’", “”", “â€˜”, “â€™”, “â€œ”]

# get the text to be processed:

theString = osax.the\_clipboard()

theWords = theString.split # convert the text to an array of the  
words

theWords.each do |item|  
charList = item.split(//) # create an array of the wordâ€™s  
characters  
# first check to see if there is an opening quote mark  
unless openingQuoteMark.include?(charList[0]) # if the word doesnâ€™t  
start with a quote mark. If it does, this will only try to capitalize  
the quote mark!  
# this works fine  
item = item.capitalize!  
else  
# but the following doesnâ€™t work  
# to test: copy me to the clipboard with “abc”  
item1 = item[0,1]  
item2 = item[1…-1]  
item = item1 + item2.capitalize!  
osax.say “but it does run!”  
end  
end

# convert the full list to a string with a space between each item

theWords = theWords \* ’ ’

# set the clipboard to the processed string

osax.set\_the\_clipboard\_to(theWords)

---

<div class="post-metadata">

**Author:** ![richardfairhurst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c37758/32.png) [@richardfairhurst](https://www.ruby-forum.com/u/richardfairhurst)\
**Post date:** [August 10, 2010, 8:34am UTC](https://www.ruby-forum.com/t/crunching-text-not-working-in-a-loop/193014/2 "2010-08-10T08:34:43Z")

</div>

On Tue, Aug 10, 2010 at 12:41 AM, Richard F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> osax.set\_the\_clipboard\_to(item) #=\> "Abc"s  
> require ‘osax’; include OSAX  
> charList = item.split(//) # create an array of the word’s  
> item1 = item[0,1]  
> item2 = item[1…-1]  
> item = item1 + item2.capitalize!  
> osax.say “but it does run!”  
> end  
> end

One thing comes to mind: you are iterating over theWords, passing each  
word into the block as the variable “item”. The object referenced to  
that variable is never modified, or the modified version assigned back  
into the theWords array. So after the “each” loop, the array the Words  
is the original one. The first branch of the unless conditional works,  
because you are calling a self modifying method on item  
(item.capitalize!). In fact, it will work even if you leave out the  
assignment, cause you are assigning to the block local variable item,  
which is then reassigned in the following iteration.

On the other branch, when you do item2 = item[1…-1] you are doing a  
copy of the string, and when you call item2.capitalize!, the original  
string is not modified:

irb(main):005:0\> s = “abcde”  
=\> “abcde”  
irb(main):006:0\> a = s[1…-1]  
=\> “bcde”  
irb(main):007:0\> a.capitalize!  
=\> “Bcde”  
irb(main):008:0\> s  
=\> “abcde”

So, the object referenced by item is not modified, and then you only  
do item = item1 + item2.capitalize!, which does nothing to the  
original string, but only assign the local variable. One way to do it  
if you don’t need the original the\_words array:

the\_words = the\_string.split

the\_words.map! do |item|  
charList = item.split(//) # create an array of the word’s  
characters  
# first check to see if there is an opening quote mark  
unless openingQuoteMark.include?(charList[0])  
item.capitalize  
else  
item1 = item[0,1]  
item2 = item[1…-1]  
item1 + item2.capitalize  
end  
end

p the\_words

map! will modify the array, substituting each entry with the result of  
the block for that entry. Notice how I don’t assign the the item  
variable anymore. And by the way, the common Ruby convention is to use  
camel\_case, not snakeCase. If you want to keep both the original array  
and the new one, you can do this:

the\_words = the\_string.split

new\_words = the\_words.map do |item|  
charList = item.split(//) # create an array of the word’s  
characters  
# first check to see if there is an opening quote mark  
unless openingQuoteMark.include?(charList[0])  
item.capitalize  
else  
item1 = item[0,1]  
item2 = item[1…-1]  
item1 + item2.capitalize  
end  
end

p the\_words  
p new\_words

map will create a new array, assigning the result of the block to each  
entry.

Hope this helps,

Jesus.

---

<div class="post-metadata">

**Author:** ![richardfairhurst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c37758/32.png) [@richardfairhurst](https://www.ruby-forum.com/u/richardfairhurst)\
**Post date:** [August 12, 2010, 12:20am UTC](https://www.ruby-forum.com/t/crunching-text-not-working-in-a-loop/193014/3 "2010-08-12T00:20:08Z")

</div>

Bless you, JesÃºs; you saved the day again!

The script now works great, and while playing with it, I learned than  
you may even have intended!

😉

THANK YOU!!

Richard F.

---

<div class="post-metadata">

**Author:** ![richardfairhurst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c37758/32.png) [@richardfairhurst](https://www.ruby-forum.com/u/richardfairhurst)\
**Post date:** [August 12, 2010, 8:37am UTC](https://www.ruby-forum.com/t/crunching-text-not-working-in-a-loop/193014/4 "2010-08-12T08:37:56Z")

</div>

2010/8/10 Jesús Gabriel y Galán [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> On Tue, Aug 10, 2010 at 12:41 AM, Richard F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
> And by the way, the common Ruby convention is to use  
> camel\_case, not snakeCase.

Notice the brainfart !

this\_is\_snake\_case  
thisIsCamelCase

The Ruby convention is to use snake\_case.

🙂

Jesus.

---

<div class="post-metadata">

**Author:** ![richardfairhurst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c37758/32.png) [@richardfairhurst](https://www.ruby-forum.com/u/richardfairhurst)\
**Post date:** [August 12, 2010, 12:23am UTC](https://www.ruby-forum.com/t/crunching-text-not-working-in-a-loop/193014/5 "2010-08-12T00:23:02Z")

</div>

> Â .Â .Â .Â I learned than

Sorry; I got too excited! That should have been:

â€œI learned more thanÂ .Â .Â .Â â€

THANK YOU!!

Richard F.
