[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Don't understand NoMethodError

Michael Lommel

7/5/2008 5:25:00 PM

Here is the code so far:

<code>
require 'rubygems'
require 'fastercsv'
require 'highline/import'


def ukquotes(the_string)
#RTF specific: converting double to single quotes
the_string.gsub! /\\'d2(.*)\\'d3/m, "\\'d4$1\\'d5"
the_string.gsub! /(\W)'/, "$1\\'d4"
the_string.gsub! /'(\W)/, "\\'d5$1"
the_string.gsub! /(\W)d"/, "$1\\'d4"
the_string.gsub! /"(\W)/, "\\'d5$1"
the_string.gsub! /(\w)'(\w)/, "$1\\'d5$2"
end

def ukspell(the_string)
#Converting US to UK spelling
csvdata=File.open("UK-US_spellingdiff.csv", "r")
FasterCSV.parse(csvdata) do |row|
usword, ukword = *row
the_string.gsub! /\b#{usword}\b/, '\b#{ukword}\b'
end
end

unless File.exists?("UK-US_spellingdiff.csv")
raise "CSV file does not exist"
break
end

if agree("You are about to perform this edit list on #{Dir.pwd}.
Proceed?", true)
directory = ask("Name of directory to write edited files to? ",
lambda{|q| q.gsub /\W/, '_'})
else
break
end

unless agree("Will write to:#{Dir.pwd}/#{directory}. Proceed?", true)
break
end


Dir.mkdir("#{Dir.pwd}/#{directory}")

Dir.glob("*.rtf").each do |filename|
newfile = File.open(filename, "r"){|f| f.read}.ukquotes.ukspell
newfilename = "CT_CW_"+ filename
File.open(directory/newfilename, "w"){|n| puts(newfile)}
end
</code>

Produces this error message:

<code>
cw-replace.rb:44: private method `ukquotes' called for
#<String:0x7755d8> (NoMethodError)
from cw-replace.rb:43:in `each'
</code>
--
Posted via http://www.ruby-....

5 Answers

Robert Klemme

7/5/2008 5:32:00 PM

0

On 05.07.2008 19:24, Michael Lommel wrote:
> Here is the code so far:
>
> <code>
> require 'rubygems'
> require 'fastercsv'
> require 'highline/import'
>
>
> def ukquotes(the_string)
> #RTF specific: converting double to single quotes
> the_string.gsub! /\\'d2(.*)\\'d3/m, "\\'d4$1\\'d5"
> the_string.gsub! /(\W)'/, "$1\\'d4"
> the_string.gsub! /'(\W)/, "\\'d5$1"
> the_string.gsub! /(\W)d"/, "$1\\'d4"
> the_string.gsub! /"(\W)/, "\\'d5$1"
> the_string.gsub! /(\w)'(\w)/, "$1\\'d5$2"
> end
>
> def ukspell(the_string)
> #Converting US to UK spelling
> csvdata=File.open("UK-US_spellingdiff.csv", "r")
> FasterCSV.parse(csvdata) do |row|
> usword, ukword = *row
> the_string.gsub! /\b#{usword}\b/, '\b#{ukword}\b'
> end
> end
>
> unless File.exists?("UK-US_spellingdiff.csv")
> raise "CSV file does not exist"
> break
> end
>
> if agree("You are about to perform this edit list on #{Dir.pwd}.
> Proceed?", true)
> directory = ask("Name of directory to write edited files to? ",
> lambda{|q| q.gsub /\W/, '_'})
> else
> break
> end
>
> unless agree("Will write to:#{Dir.pwd}/#{directory}. Proceed?", true)
> break
> end
>
>
> Dir.mkdir("#{Dir.pwd}/#{directory}")
>
> Dir.glob("*.rtf").each do |filename|
> newfile = File.open(filename, "r"){|f| f.read}.ukquotes.ukspell

^^^ error line

> newfilename = "CT_CW_"+ filename
> File.open(directory/newfilename, "w"){|n| puts(newfile)}
> end
> </code>
>
> Produces this error message:
>
> <code>
> cw-replace.rb:44: private method `ukquotes' called for
> #<String:0x7755d8> (NoMethodError)
> from cw-replace.rb:43:in `each'
> </code>

You're defining #ukquotes in main (like a global function) but you use
it as an instance method. Either place it in class String or call it
like a function.

Cheers

robert

Robert Klemme

7/5/2008 5:33:00 PM

0

On 05.07.2008 19:24, Michael Lommel wrote:
> Here is the code so far:
>
> <code>
> require 'rubygems'
> require 'fastercsv'
> require 'highline/import'
>
>
> def ukquotes(the_string)
> #RTF specific: converting double to single quotes
> the_string.gsub! /\\'d2(.*)\\'d3/m, "\\'d4$1\\'d5"
> the_string.gsub! /(\W)'/, "$1\\'d4"
> the_string.gsub! /'(\W)/, "\\'d5$1"
> the_string.gsub! /(\W)d"/, "$1\\'d4"
> the_string.gsub! /"(\W)/, "\\'d5$1"
> the_string.gsub! /(\w)'(\w)/, "$1\\'d5$2"
> end
>
> def ukspell(the_string)
> #Converting US to UK spelling
> csvdata=File.open("UK-US_spellingdiff.csv", "r")
> FasterCSV.parse(csvdata) do |row|
> usword, ukword = *row
> the_string.gsub! /\b#{usword}\b/, '\b#{ukword}\b'
> end
> end
>
> unless File.exists?("UK-US_spellingdiff.csv")
> raise "CSV file does not exist"
> break
> end
>
> if agree("You are about to perform this edit list on #{Dir.pwd}.
> Proceed?", true)
> directory = ask("Name of directory to write edited files to? ",
> lambda{|q| q.gsub /\W/, '_'})
> else
> break
> end
>
> unless agree("Will write to:#{Dir.pwd}/#{directory}. Proceed?", true)
> break
> end
>
>
> Dir.mkdir("#{Dir.pwd}/#{directory}")
>
> Dir.glob("*.rtf").each do |filename|
> newfile = File.open(filename, "r"){|f| f.read}.ukquotes.ukspell

^^^ error line

> newfilename = "CT_CW_"+ filename
> File.open(directory/newfilename, "w"){|n| puts(newfile)}
> end
> </code>
>
> Produces this error message:
>
> <code>
> cw-replace.rb:44: private method `ukquotes' called for
> #<String:0x7755d8> (NoMethodError)
> from cw-replace.rb:43:in `each'
> </code>

You're defining #ukquotes in main (like a global function) but you use
it as an instance method. Either place it in class String or call it
like a function.

Cheers

robert

Robert Klemme

7/5/2008 5:33:00 PM

0

On 05.07.2008 19:24, Michael Lommel wrote:
> Here is the code so far:
>
> <code>
> require 'rubygems'
> require 'fastercsv'
> require 'highline/import'
>
>
> def ukquotes(the_string)
> #RTF specific: converting double to single quotes
> the_string.gsub! /\\'d2(.*)\\'d3/m, "\\'d4$1\\'d5"
> the_string.gsub! /(\W)'/, "$1\\'d4"
> the_string.gsub! /'(\W)/, "\\'d5$1"
> the_string.gsub! /(\W)d"/, "$1\\'d4"
> the_string.gsub! /"(\W)/, "\\'d5$1"
> the_string.gsub! /(\w)'(\w)/, "$1\\'d5$2"
> end
>
> def ukspell(the_string)
> #Converting US to UK spelling
> csvdata=File.open("UK-US_spellingdiff.csv", "r")
> FasterCSV.parse(csvdata) do |row|
> usword, ukword = *row
> the_string.gsub! /\b#{usword}\b/, '\b#{ukword}\b'
> end
> end
>
> unless File.exists?("UK-US_spellingdiff.csv")
> raise "CSV file does not exist"
> break
> end
>
> if agree("You are about to perform this edit list on #{Dir.pwd}.
> Proceed?", true)
> directory = ask("Name of directory to write edited files to? ",
> lambda{|q| q.gsub /\W/, '_'})
> else
> break
> end
>
> unless agree("Will write to:#{Dir.pwd}/#{directory}. Proceed?", true)
> break
> end
>
>
> Dir.mkdir("#{Dir.pwd}/#{directory}")
>
> Dir.glob("*.rtf").each do |filename|
> newfile = File.open(filename, "r"){|f| f.read}.ukquotes.ukspell

^^^ error line

> newfilename = "CT_CW_"+ filename
> File.open(directory/newfilename, "w"){|n| puts(newfile)}
> end
> </code>
>
> Produces this error message:
>
> <code>
> cw-replace.rb:44: private method `ukquotes' called for
> #<String:0x7755d8> (NoMethodError)
> from cw-replace.rb:43:in `each'
> </code>

You're defining #ukquotes in main (like a global function) but you use
it as an instance method. Either place it in class String or call it
like a function.

Cheers

robert

Robert Klemme

7/5/2008 5:34:00 PM

0

On 05.07.2008 19:33, Robert Klemme wrote:

Sorry for the noise. My newsreader fooled my.

robert

James Gray

7/5/2008 5:41:00 PM

0

On Jul 5, 2008, at 12:24 PM, Michael Lommel wrote:

> Here is the code so far:

> newfile = File.open(filename, "r"){|f| f.read}.ukquotes.ukspell

> Produces this error message:
>
> <code>
> cw-replace.rb:44: private method `ukquotes' called for
> #<String:0x7755d8> (NoMethodError)
> from cw-replace.rb:43:in `each'
> </code>

In the line shown above, you read the contents of a file into a
String, then try to call the ukquotes() method on that String.

You didn't define that method for Strings though. Instead, you
defined it to have the String passed in as an argument.

Thus, you probably want to rewrite that line as something like:

newfile = ukspell(ukquotes(File.read(filename)))

I hope that helps.

James Edward Gray II