On Sep 27, 2007, at 3:34 PM, Luis Enrique wrote:

> Hi,
> I am new to Ruby and come form C language.
> Can someone pint out for me my mistake please.
> It seem not to find the file ever no regardless of the path.
>
> class Scan
>   attr_reader :filename, :alpha_count, :digit_count,
> :non_printable_count, :line_count, :word_count, :symbol_count
>   attr_writer :filename, :alpha_count, :digit_count,
> :non_printable_count, :line_count, :word_count, :symbol_count
>   filed_ptr=0
>
>   def initialize(filename)
>     @filename=filename
>     @alpha_count=0
>     @digit_count=0
>     @symbol_count=0
>     @non_printable_count=0
>     @blank_count=0
>     @line_count=0
>     @word_count=0
>   end #initialize
>
>   def open_count
>       printf( "filenamae= %s\n",@filename)
>
>   file_ptr=File.open(@filename,"r") do |file|
>       file.each_byte do |ch|
>         case ch.to_i
>         when 32
>           @blank +=1
>         when 0..31
>           @non_printtable_count+=1
>         when 48..57
>           @digit_count +=1
>         when 65..90 ,97..122
>           @alpha_count +=1
>         when -1
>           puts "Found End of file"
>         else  @symbol_count +=1
>         end #case
>       end #each_byte
>       file_ptr.close
>     end #open
>   end #open_count
> end #class
>
> print "Enter the path and file name ej. /home/user1/file.txt  :->"
> fname=gets
> ff=Scan.new(fname)
> ff.open_count()

Here are some other ways you can make your code more Ruby-like:

<code>
#! /usr/bin/env ruby -w

class Scan
    # use attr_accessor instead of attr_reader and attr_writer
    attr_accessor :attr_names, :filename, :alpha_count, :digit_count
    attr_accessor :non_printable_count, :line_count, :word_count
    attr_accessor :symbol_count

    # filed_ptr=0 <-- not needed

    def initialize(filename)
       @filename=filename
       @alpha_count=0
       @digit_count=0
       @symbol_count=0
       @non_printable_count=0
       @blank_count=0
       @line_count=0
       @word_count=0
    end #initialize

    def open_count
       printf( "filename = %s\n",@filename)
       File.open(@filename) do |file|  # <-- "r" not needed, is default
          file.each_byte do |ch|
             case ch # <-- to_i not needed
             when 32
                @blank_count+=1
             when 0..31
                @non_printable_count+=1
             when 48..57
                @digit_count+=1
             when 65..90,97..122
                @alpha_count+=1
             when -1
                puts "Found End of file"
             else  @symbol_count+=1
             end #case
          end #each_byte
          # File.open given a block ensures file will be closed
          # file_ptr.close <-- not needed
       end #open
    end #open_count
end #class

print "Enter the path and file name (e.g. /home/user1/file.txt) :->"
s = Scan.new(gets.chomp)
s.open_count # <-- () not needed
p s
</code>

<result>
filename = /Users/mg/Desktop/test.rb
#<Scan:0x24450 @alpha_count=715, @non_printable_count=49, @filename="/ 
Users/mg/Desktop/test.rb", @word_count=0, @symbol_count=162,  
@line_count=0, @digit_count=33, @blank_count=389>
</result>

Regards, Morton