PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > read file and print contents - beginner
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
read file and print contents - beginner

Réponse
 
LinkBack Outils de la discussion
Vieux 03/12/2007, 15h57   #1
Johnathan Smith
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut read file and print contents - beginner

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 .

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/.

  Réponse avec citation
Vieux 03/12/2007, 16h07   #2
Christian von Kleist
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: read file and print contents - beginner

I would change

if $indent > 0
(1..$indent).each { print ' ' }
end

to

print ' ' * $indent

On Dec 3, 2007 10:57 AM, Johnathan Smith <stu_09@hotmail.com> wrote:
> 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 .
>
> 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/.
>
>


  Réponse avec citation
Vieux 03/12/2007, 16h16   #3
Johnathan Smith
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: read file and print contents - beginner

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/.

  Réponse avec citation
Vieux 03/12/2007, 16h42   #4
Christian von Kleist
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: read file and print contents - beginner

On Dec 3, 2007 11:16 AM, Johnathan Smith <stu_09@hotmail.com> 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
>
> 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/.
>
>


You're missing two `end` lines:

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

Sorry, I thought we were just looking at a fragment of your code
before, so I didn't comment on the missing `end` lines.

  Réponse avec citation
Vieux 03/12/2007, 17h08   #5
Andrei Maxim
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: read file and print contents - beginner

On 12/3/07, Johnathan Smith <stu_09@hotmail.com> 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

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h24.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10964 seconds with 13 queries