1

Some of you may notice I'm already back with the same painful code already. I'm not sure if the other question is still open or not once I accept an answer.

Now the problem is a little simpler. I found some code that checked for pangrams. It use to be def pangram?('sentence') but I needed line to go in there so I tried changing it to def pangram?(line). It doesn't seem to mesh well with my coding style and doesn't work. I tried to use .contain('a' . . 'z') to check for a pangram but someone I know tried that and it didn't work. Also google isn't much help either.

Any ideas for how I could check for pangrams in an if stmt?

# To change this template, choose Tools | Templates
# and open the template in the editor
# This program reads a file line by line,
#separating lines by writing into certain text files.
#PPQ - Pangrams, Palindromes, and Quotes
class PPQ
  def pangram?(line)
    unused_letters = ('a'..'z').to_a - line.downcase.chars.to_a
    unused_letters.empty?
  end
  def categorize
    file_pangram = File.new('pangram.txt', 'w')
    file_palindrome = File.new('palindrome.txt', 'w')
    file_quotes = File.new('quotes.txt','w')
    File.open('ruby1.txt','r') do |file|
      while line = file.gets
        if(line.reverse == line)
          file_palindrome.write line
        elsif(pangram?(line)== true)
          file_pangram.write line
        else
          file_quotes.write line
        end
      end
    end
    file.close
    file_pangram.close
    file_palindrome.close
    file_quotes.close
   end
end
my_ruby_assignment = PPQ.new
my_ruby_assignment.categorize
  • You should really work on reducing the code in your questions to an [SSCCE](http://sscce.org). – Matt Ball Apr 26 '12 at 02:54
  • Also, that `pangram?()` implementation ([straight from Rosetta Code](http://rosettacode.org/wiki/Pangram_checker#Ruby), btw) works just fine, so the problem is elsewhere. http://ideone.com/HHbfi – Matt Ball Apr 26 '12 at 03:01
  • Sorry about that I messed something up when posting the question. and was thinking my code looked terrible. It took away my indentation. – Nathaniel Ratliff Apr 26 '12 at 03:46
  • I don't see what that has to do with the formatting. Like I said, the `pangram?()` implementation works. – Matt Ball Apr 26 '12 at 03:53

2 Answers2

5

I'm partial to simpler syntax, something like

def pangram?(line)
  ('a'..'z').all? { |word| line.downcase.include? (word) }
end

if pangram?(line) then file_pangram.write line end
0
def pangram?(string)
  str = string.chars.map(&:downcase)
  letters =('a'..'z').to_a
  result = true
  letters.each do |l|
    if !(str.include? l.downcase)
      result = false
      break
    end
  end

  result
end
Jagdish
  • 752
  • 8
  • 23