From: Richard Fairbanks on 9 Aug 2010 18:41 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 Fairbanks ---- 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) -- Posted via http://www.ruby-forum.com/.
From: Jesús Gabriel y Galán on 10 Aug 2010 02:34 On Tue, Aug 10, 2010 at 12:41 AM, Richard Fairbanks <lists(a)f-p-i.com> wrote: > 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 doesnt 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 Fairbanks > > ---- > > The following does work, but only if the first quote is a straight > quote: ' or ". It doesnt 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 doesnt 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 words > characters > # first check to see if there is an opening quote mark > unless openingQuoteMark.include?(charList[0]) # if the word doesnt > 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 doesnt 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 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 words 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 words 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.
From: Richard Fairbanks on 11 Aug 2010 18:20 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 Fairbanks -- Posted via http://www.ruby-forum.com/.
From: Richard Fairbanks on 11 Aug 2010 18:23 > . . . I learned than Sorry; I got too excited! That should have been: “I learned more than . . . ” THANK YOU!! Richard Fairbanks -- Posted via http://www.ruby-forum.com/.
From: Jesús Gabriel y Galán on 12 Aug 2010 02:27 2010/8/10 Jesús Gabriel y Galán <jgabrielygalan(a)gmail.com>: > On Tue, Aug 10, 2010 at 12:41 AM, Richard Fairbanks <lists(a)f-p-i.com> 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.
|
Pages: 1 Prev: Error: Bignum too big to convert into `long' Next: haml view can't find helper function |