# Don't understand NoMethodError

**URL:** <https://www.ruby-forum.com/t/dont-understand-nomethoderror/142275>\
**Category:** Ruby\
**Created:** [July 5, 2008, 7:27pm UTC](https://www.ruby-forum.com/t/dont-understand-nomethoderror/142275 "2008-07-05T19:27:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [July 5, 2008, 7:27pm UTC](https://www.ruby-forum.com/t/dont-understand-nomethoderror/142275/1 "2008-07-05T19:27:59Z")

</div>

Here is the code so far:

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

Produces this error message:

`
cw-replace.rb:44: private method `ukquotes' called for
# (NoMethodError)
        from cw-replace.rb:43:in `each'
`

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [July 5, 2008, 7:36pm UTC](https://www.ruby-forum.com/t/dont-understand-nomethoderror/142275/2 "2008-07-05T19:36:08Z")

</div>

On 05.07.2008 19:24, Michael L. wrote:

> the\_string.gsub! /\'d2(.\*)\'d3/m, “\'d4$1\'d5”  
> FasterCSV.parse(csvdata) do |row|  
> if agree("You are about to perform this edit list on #{Dir.pwd}.
> 
> Dir.mkdir("#{Dir.pwd}/#{directory}")
> 
> Dir.glob("\*.rtf").each do |filename|  
> newfile = File.open(filename, “r”){|f| f.read}.ukquotes.ukspell

^^^ error line

> ```
> from cw-replace.rb:43:in `each'
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [July 5, 2008, 7:39pm UTC](https://www.ruby-forum.com/t/dont-understand-nomethoderror/142275/3 "2008-07-05T19:39:38Z")

</div>

On 05.07.2008 19:33, Robert K. wrote:

Sorry for the noise. My newsreader fooled my.

robert

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [July 5, 2008, 7:44pm UTC](https://www.ruby-forum.com/t/dont-understand-nomethoderror/142275/4 "2008-07-05T19:44:55Z")

</div>

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

> Here is the code so far:

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

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

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 G. II
