Don't understand NoMethodError

M

Michael Lommel

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>
 
R

Robert Klemme

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
 
R

Robert Klemme

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
 
R

Robert Klemme

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
 
R

Robert Klemme

On 05.07.2008 19:33, Robert Klemme wrote:

Sorry for the noise. My newsreader fooled my.

robert
 
J

James Gray

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top