School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,427 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,492 people online right now. Registration is fast and FREE... Join Now!




[File] in ruby

 

[File] in ruby

Cs Student

22 May, 2009 - 11:51 PM
Post #1

New D.I.C Head
*

Joined: 9 Jan, 2009
Posts: 11


My Contributions


hi biggrin.gif


how i can open text file in ruby and count the number of characters in it ?




User is offlineProfile CardPM
+Quote Post


vikhyat

RE: [File] In Ruby

23 May, 2009 - 01:45 AM
Post #2

New D.I.C Head
*

Joined: 14 Feb, 2009
Posts: 5



Thanked: 1 times
My Contributions
You'd want to look at these-
http://www.ruby-doc.org/core/classes/File.html
http://pleac.sourceforge.net/pleac_ruby/filecontents.html
http://numericalruby.com/2007/06/22/readin...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.

CODE
file_contents = File.new(filename).read


This post has been edited by vikhyat: 23 May, 2009 - 01:46 AM
User is offlineProfile CardPM
+Quote Post

jcvaldezo

RE: [File] In Ruby

12 Jun, 2009 - 11:55 AM
Post #3

New D.I.C Head
*

Joined: 11 Jun, 2009
Posts: 1



Thanked: 1 times
My Contributions
QUOTE(vikhyat @ 23 May, 2009 - 01:45 AM) *

You'd want to look at these-
http://www.ruby-doc.org/core/classes/File.html
http://pleac.sourceforge.net/pleac_ruby/filecontents.html
http://numericalruby.com/2007/06/22/readin...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.

CODE
file_contents = File.new(filename).read






No need to open the file if yo use:

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


substituting search.out for your file's name
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 12:25AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month