[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

File.Open

Luis Enrique

9/27/2007 7:35:00 PM

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()
--
Posted via http://www.ruby-....

7 Answers

Lionel Bouton

9/27/2007 8:05:00 PM

0

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

Luis Enrique

9/27/2007 8:14:00 PM

0

Yes!

Thanks.

Lionel
>
> Quick guess :
> fname=get.chomp

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

Morton Goldberg

9/28/2007 1:18:00 AM

0

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

Luis Enrique

9/28/2007 7:51:00 PM

0

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
--
Posted via http://www.ruby-....

Luis Enrique

9/28/2007 8:33:00 PM

0

Jason Roelofs 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-....

Luis Enrique

9/28/2007 10:10:00 PM

0


>> --
>> Posted via http://www.ruby-....
>>
>>
> 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
--
Posted via http://www.ruby-....

Croata

9/28/2007 10:23:00 PM

0

2007/9/28, Jason Roelofs <jameskilton@gmail.com>:
> isalpha: /[a-zA-Z]/
> isspace: /\w/
>
> Regular Expressions are your friend.
>
> Jason
>

Just a little typo:

isspace: /\s/

--
Regards,
Cro