Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > read file and print contents - beginner

Reply
Thread Tools

read file and print contents - beginner

 
 
Johnathan Smith
Guest
Posts: n/a
 
      12-03-2007
hello,

im new to ruby and i have a text file and want to read in the file
and print it out.

so far iv got the following. I'd greatly appreciate any help.

thanks.

text file (reference.txt):
Tag: ref1
Type: Book
Author: Little, S R

ruby file:
#!/usr/local/bin/ruby
#
#
# read file and print
#
ARGV.each do |fn|
begin
(fn == '-' ? STDIN : File.open(fn)).each_line do |l|
if $indent > 0
(1..$indent).each { print ' ' }
end
puts l
end
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Johnathan Smith
Guest
Posts: n/a
 
      12-03-2007
I've changed my approach as i dont actually want to count the lines

so i now have this:

ARGV.each do |fn|
begin
(fn == 'reference.txt' ? STDIN : File.open(fn)).each_line do |l|
puts l
end

by this im trying to read in the text file and print out its contents

i seem to be getting a load error
any reason why?

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

 
Reply With Quote
 
 
 
 
Andrei Maxim
Guest
Posts: n/a
 
      12-03-2007
On 12/3/07, Johnathan Smith <(E-Mail Removed)> wrote:
> I've changed my approach as i dont actually want to count the lines
>
> so i now have this:
>
> ARGV.each do |fn|
> begin
> (fn == 'reference.txt' ? STDIN : File.open(fn)).each_line do |l|
> puts l
> end


ARGV.each will iterate through every parameter you pass. Since your
script is so simple, you're better of with ARGV[0]. You'd have to
check the length and see if ARGV.length == 1.

A more Ruby-like approach is this:

#!/usr/bin/env ruby -wKU

if ARGV.length != 1
puts "Syntax is: ruby readfile.rb filename"
exit
end

File.open(ARGV[0], "r") do |file|
while line = file.gets
puts line
end
end

Using File.open with a block will automatically open and close the
file handler and that's a pretty decent practice to start with.

I'd highly recommend you the PickAxe book (Programming Ruby, 2nd
edition). It does a great job explaining Ruby concepts (the code above
is just a rip-off from Mr. Thomas's example on page 129). However,
I've heard people complaining that it's a bit daunting for new
programmers. Maybe you'd feel a bit better with Learning to Program by
Chris Pine if words like "iterators" and "inheritance" make you sweat.
--
Andrei Maxim
http://andreimaxim.ro

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding contents on yaml file without overwriting actual contents Kamarulnizam Rahim Ruby 4 01-28-2011 09:10 AM
Problem - I want to print Current Output of Pdf file and should print once.I get print dialog box but it is not working keto Java 0 05-30-2007 11:27 AM
Beginner: read and print same file Marek Stepanek Perl Misc 6 08-14-2006 02:35 AM
Unlarging the print to print using PDF file to print Bun Mui Computer Support 3 09-13-2004 03:15 AM
Can I restrict both attribute contents and element contents in schema Don Adams XML 1 03-05-2004 12:48 PM



Advertisments