[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Setting the contents of a file to a variable?

poison tooth

2/27/2009 1:51:00 AM

[Note: parts of this message were removed to make it a legal post.]

Ok so...
Ok... so I have file1 and it contains a number
and I want file2 to tell me what that number is
file2 would be something like

class Tell
def get_num(file = "file1.txt")
num = contents of file1
$num = num
end
def tell_num
puts "The number in file1 is #{$num}."
end
end

What do I put instead of "contents of file1"
Or some completely new code that does the same thing.

--
There was a man, they called him mad.
The more he gave, the more he had.

15 Answers

Justin Collins

2/27/2009 2:38:00 AM

0

Stefan Codrescu wrote:
> Ok so...
> Ok... so I have file1 and it contains a number
> and I want file2 to tell me what that number is
> file2 would be something like
>
> class Tell
> def get_num(file = "file1.txt")
> num = contents of file1
> $num = num
> end
> def tell_num
> puts "The number in file1 is #{$num}."
> end
> end
>
> What do I put instead of "contents of file1"
> Or some completely new code that does the same thing.
>
>

File.read file


You probably also want $num to be @num instead (that is, an instance
variable rather than a global variable).

-Justin

poison tooth

3/6/2009 10:58:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

ok, thanks ill try that but i also found
file = File.open("guess.txt")
$guess = file.gets
which works for what im using it for.

On Thu, Feb 26, 2009 at 7:38 PM, Justin Collins <justincollins@ucla.edu>wrote:

> Stefan Codrescu wrote:
>
>> Ok so...
>> Ok... so I have file1 and it contains a number
>> and I want file2 to tell me what that number is
>> file2 would be something like
>>
>> class Tell
>> def get_num(file = "file1.txt")
>> num = contents of file1
>> $num = num
>> end
>> def tell_num
>> puts "The number in file1 is #{$num}."
>> end
>> end
>>
>> What do I put instead of "contents of file1"
>> Or some completely new code that does the same thing.
>>
>>
>>
>
> File.read file
>
>
> You probably also want $num to be @num instead (that is, an instance
> variable rather than a global variable).
>
> -Justin
>
>


--
There was a man, they called him mad.
The more he gave, the more he had.

Brian Candler

3/8/2009 7:34:00 PM

0

Stefan Codrescu wrote:
> ok, thanks ill try that but i also found
> file = File.open("guess.txt")
> $guess = file.gets
> which works for what im using it for.

BTW, you forgot to close the file. Although that doesn't matter in a
short-lived script, there is a way of avoiding this problem:

File.open("guess.txt") do |file|
$guess = file.gets
end

This opens the file, runs the block, and then closes the file (even if
the block aborted with an exception rather than completing successfully)

BTW, local variables which are first seen within a block are private to
that block, so if you want do use a local variable do this:

guess = nil
File.open("guess.txt") do |file|
guess = file.gets
end
... now 'guess' contains the result of reading the 1st line of file
--
Posted via http://www.ruby-....

Jan-Erik R.

3/8/2009 7:37:00 PM

0

Brian Candler schrieb:
> Stefan Codrescu wrote:
>> ok, thanks ill try that but i also found
>> file = File.open("guess.txt")
>> $guess = file.gets
>> which works for what im using it for.
>
> BTW, you forgot to close the file. Although that doesn't matter in a
> short-lived script, there is a way of avoiding this problem:
>
> File.open("guess.txt") do |file|
> $guess = file.gets
> end
>
> This opens the file, runs the block, and then closes the file (even if
> the block aborted with an exception rather than completing successfully)
>
> BTW, local variables which are first seen within a block are private to
> that block, so if you want do use a local variable do this:
>
> guess = nil
> File.open("guess.txt") do |file|
> guess = file.gets
> end
> ... now 'guess' contains the result of reading the 1st line of file
if you just need the content, use File.read, if you need the lines in an
Array use File.readlines

Matthias Reitinger

3/8/2009 9:17:00 PM

0

Brian Candler wrote:
> BTW, local variables which are first seen within a block are private to
> that block, so if you want do use a local variable do this:
>
> guess = nil
> File.open("guess.txt") do |file|
> guess = file.gets
> end
> ... now 'guess' contains the result of reading the 1st line of file

Or even this:

guess = File.open("guess.txt") do |file|
file.gets
end

Maybe even this (Symbol#to_proc implementation/Ruby 1.9 required):

guess = File.open("guess.txt", &:gets)

-Matthias

--
Posted via http://www.ruby-....

Jan-Erik R.

3/9/2009 10:45:00 AM

0

Matthias Reitinger schrieb:
> Brian Candler wrote:
>> BTW, local variables which are first seen within a block are private to
>> that block, so if you want do use a local variable do this:
>>
>> guess = nil
>> File.open("guess.txt") do |file|
>> guess = file.gets
>> end
>> ... now 'guess' contains the result of reading the 1st line of file
>
> Or even this:
>
> guess = File.open("guess.txt") do |file|
> file.gets
> end
>
> Maybe even this (Symbol#to_proc implementation/Ruby 1.9 required):
>
> guess = File.open("guess.txt", &:gets)
>
> -Matthias
>
did you read my post? why should we use a construct with a block if
there are several methods to do this directly?

Matthias Reitinger

3/9/2009 11:12:00 AM

0

badboy wrote:
> Matthias Reitinger schrieb:
>> Or even this:
>>
> did you read my post? why should we use a construct with a block if
> there are several methods to do this directly?

If you just want the first line of a file (and that's what I was
specifically refering to) then reading in the whole file might not be
the best idea.

-Matthias

--
Posted via http://www.ruby-....

Torli Birnbauer

3/9/2009 4:11:00 PM

0

badboy wrote:
> did you read my post? why should we use a construct with a block if
> there are several methods to do this directly?

Because the block way is the Ruby way, besides it's much more efficient
it does open, read and close in one short command! Beside it its
elegance it also has a pedagogical value, namely, it highlights and
exposes the use of Ruby 1.9 usage of Symbol class to_proc strategy. So
all I can say is: Matthias, excellent contribution to this thread, very
good job.

Perhaps only a comment that even reading multi-line file into a variable
sometimes can be useful with:

guess = File.open("zorba.data", &:readlines)

Clearly the old way in comparison to the the block is more like the
spaghetti code!

Torli
--
Posted via http://www.ruby-....

Robert Klemme

3/9/2009 4:47:00 PM

0

2009/3/9 Torli Birnbauer <aipir@sympatico.ca>:
> badboy wrote:
>> did you read my post? why should we use a construct with a block if
>> there are several methods to do this directly?
>
> Because the block way is the Ruby way, besides it's much more efficient
> it does open, read and close in one short command! Beside it its
> elegance it also has a pedagogical value, namely, it highlights and
> exposes the use of Ruby 1.9 usage of Symbol class to_proc strategy. So
> all I can say is: Matthias, excellent contribution to this thread, very
> good job.

Frankly, I believe you haven't properly read badboy's posting. I'll
show his code below.

> Perhaps only a comment that even reading multi-line file into a variable
> sometimes can be useful with:
>
> =A0guess =3D File.open("zorba.data", &:readlines)
>
> Clearly the old way in comparison to the the block is more like the
> spaghetti code!

Like these?

17:45:12 tmp$ cat >| x <<EOF
> 123
> 456
> EOF
17:45:23 tmp$ cat x
123
456
17:45:25 tmp$ allruby -e 'p File.read("x"), File.readlines("x")'
ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin]
"123\n456\n"
["123\n", "456\n"]
ruby 1.9.1p0 (2009-01-30 revision 21907) [i386-cygwin]
"123\n456\n"
["123\n", "456\n"]
17:45:55 tmp$

Very spaghetti indeed.

Cheers

robert


PS: This posting contains traces of irony. Please handle with care.

--=20
remember.guy do |as, often| as.you_can - without end

Torli Birnbauer

3/9/2009 6:17:00 PM

0

Robert Klemme wrote:
> tmp$ cat >| x <<EOF

All programming languages can be abused. This does not make the abuser a
hero!

PS: whose main message is in the "ps" addendum must have missed the
point totally!

Torli
--
Posted via http://www.ruby-....