2 Replies - 2037 Views - Last Post: 12 June 2009 - 12:55 PM Rate Topic: -----

#1 Cs Student   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 10-January 09

[File] in ruby

Posted 23 May 2009 - 12:51 AM

hi :D


how i can open text file in ruby and count the number of characters in it ?
Is This A Good Question/Topic? 0
  • +

Replies To: [File] in ruby

#2 vikhyat   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 15-February 09

Re: [File] in ruby

Posted 23 May 2009 - 02:45 AM

You'd want to look at these-
http://www.ruby-doc....asses/File.html
http://pleac.sourcef...lecontents.html
http://numericalruby...file-with-ruby/

One way of doing it would be to open the file and read it line-by-line, counting the number of characters in each line as you go. An easier way would be to open the file, read all of it's contents into a string and get the length of the string.

file_contents = File.new(filename).read

This post has been edited by vikhyat: 23 May 2009 - 02:46 AM

Was This Post Helpful? 0
  • +
  • -

#3 jcvaldezo   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 11-June 09

Re: [File] in ruby

Posted 12 June 2009 - 12:55 PM

View Postvikhyat, on 23 May, 2009 - 01:45 AM, said:

You'd want to look at these-
http://www.ruby-doc....asses/File.html
http://pleac.sourcef...lecontents.html
http://numericalruby...file-with-ruby/

One way of doing it would be to open the file and read it line-by-line, counting the number of characters in each line as you go. An easier way would be to open the file, read all of it's contents into a string and get the length of the string.

file_contents = File.new(filename).read





No need to open the file if yo use:

f = File.size("../search.out")


substituting search.out for your file's name
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1