File.Open

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()

Luis E. wrote the following on 27.09.2007 21:34 :

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.
[…]
print “Enter the path and file name ej. /home/user1/file.txt :->”
fname=gets

Quick guess :
fname=get.chomp

Yes!

Thanks.

Lionel

Quick guess :
fname=get.chomp

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

filed_ptr=0
end #initialize
@non_printtable_count+=1
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:

#! /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

filename = /Users/mg/Desktop/test.rb #

Regards, Morton

isalpha: /[a-zA-Z]/
isspace: /\w/

Regular Expressions are your friend.

Jason

Morton thank you for the advice.

Does Ruby have an equivalent function/method to the C isalpha, isspace
and etc.
all included in #include <ctype.h>?

-L

Jason R. wrote:

isalpha: /[a-zA-Z]/
isspace: /\w/

Regular Expressions are your friend.

Jason

Thanks Jason.
That works but how to put the non printable characters in there?

-L


Posted via http://www.ruby-forum.com/.

Hmm, not sure right off hand. What exactly are you trying to do? search
for
certain instances of hex data?

Jason

I am trying to port a parser from c to Ruby. It is a very rudimentary
scanner and there are around 50 tokens.
From my bad code on the first post you can notice I was trying to use
ascii table/code. But it does not get me the same results as my c code.

-L

2007/9/28, Jason R. [email protected]:

isalpha: /[a-zA-Z]/
isspace: /\w/

Regular Expressions are your friend.

Jason

Just a little typo:

isspace: /\s/

On 9/28/07, Luis E. [email protected] wrote:

That works but how to put the non printable characters in there?

-L

Posted via http://www.ruby-forum.com/.

Hmm, not sure right off hand. What exactly are you trying to do? search
for
certain instances of hex data?

Jason

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs