File.Open

L

Luis Enrique

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

Lionel Bouton

Luis Enrique 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
 
M

Morton Goldberg

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
 
L

Luis Enrique

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
 
L

Luis Enrique

Jason said:
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
 
L

Luis Enrique

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
 

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

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top