read file and print contents - beginner

Discussion in 'Ruby' started by Johnathan Smith, Dec 3, 2007.

  1. 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/.
     
    Johnathan Smith, Dec 3, 2007
    #1
    1. Advertisements

  2. 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/.
     
    Johnathan Smith, Dec 3, 2007
    #2
    1. Advertisements

  3. Johnathan Smith

    Andrei Maxim Guest

    On 12/3/07, Johnathan Smith <> 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
     
    Andrei Maxim, Dec 3, 2007
    #3
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Holliday

    Upload file and read contents from the stream?

    Chris Holliday, Jun 26, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    679
    -=Chris=-
    Nov 16, 2003
  2. Don Adams
    Replies:
    1
    Views:
    788
    Martin Honnen
    Mar 5, 2004
  3. keto
    Replies:
    0
    Views:
    1,381
  4. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    646
    David Cournapeau
    Dec 30, 2008
  5. Replies:
    3
    Views:
    213
    Brian Candler
    Dec 25, 2008
  6. Alex Dowad
    Replies:
    4
    Views:
    689
    Michel Demazure
    May 1, 2010
  7. Kamarulnizam Rahim
    Replies:
    4
    Views:
    449
    Robert Klemme
    Jan 28, 2011
  8. Marek Stepanek

    Beginner: read and print same file

    Marek Stepanek, Aug 13, 2006, in forum: Perl Misc
    Replies:
    6
    Views:
    226
    John W. Krahn
    Aug 14, 2006
Loading...