Assigning .read Results to a Variable? (Learn Ruby The Hard Way Ex. 15/16)

125 Views Asked by At

I'm brand new to programming and I'm working on Learn Ruby The Hard Way. In Exercise 15 he shows how to open and read a text file, and how to print that to the screen. In the next exercise, he briefly mentions at the beginning that the '.read' command "Reads the contents of a file. You can assign the result to a variable."

All I want to know is HOW to assign the result of a read to a new variable. In exercise 16 he doesn't use it and just keeps going into writing. I wrote a short script to read a text file containing a number, then '.to_i' that number and return a boolean value. I know I'm missing something, and it doesn't add up to me.

Basically all I need to know is how to assign the txt.read output to a new variable (simply called, 'variable') and then use gets.chomp.to_i to convert it to an integer so the boolean comparison works. Any help is appreciated, thank you!

print "Enter the filename: "
filename = gets.chomp

txt = open(filename)
print txt.read
variable = txt.read.to_i ##Trying to assign the txt.read to a variable?

print { variable > 4 && false }
3

There are 3 best solutions below

2
Paweł Obrok On

When opening a file a cursor is created to the point in that file we have read to. Calling read without any extra arguments reads the whole file and points that cursor to the end of the file, so any subsequent read calls will return an empty string (""). In your case you would need to assign first and then do anything else:

txt = open(filename)
content = txt.read
print content
variable = content.to_i

You can compare this with calling read many times with an argument signifying the number of bytes to read. You will get another chunk of the file of the specified length each time.

Also your final call is probably problematic - print does not take a block and that's what you're passing. You just need to print(variable > 4) or something similar.

0
steenslag On
print "Enter the filename: "
filename = gets.chomp

file = open(filename)
variable = file.read
file.close
number = variable.to_i
1
shevy On

I'm working on Learn Ruby The Hard Way.

Be careful with this book - it was written by someone who writes more Python code than Ruby code.

This will unavoidably affect the idiomatic ways to do something in Ruby.

Exercise 15 shows how to open and read a text file

In general, this is easy:

data = File.readlines('foo.txt')
data = File.read('foo.txt')

It also is good practice to do a:

if File.exist? 'foo.txt'

check prior to that, as otherwise the above may fail.

and how to print that to the screen

Also simple. Once you have the handle, through the variable, you can just output it.

If you like to debug, I recommend to use pp:

require 'pp'
pp data # From the above variable name.

In the next exercise, he briefly mentions at the beginning that the '.read' command "Reads the contents of a file. You can assign the result to a variable."

Yes, that is correct. .read() is a method that you can use on File, but also IO-related, objects.

All I want to know is HOW to assign the result of a read to a new variable.

Just as always, simply use the = character for assignment.

You can assign to the return-value of a method too, which in the above cases, will be an Array or a String.

With an Array you may have it easier to manipulate things, but it is simple to convert from String to Array and back again anyway. Keep in mind that big files may cause a slight speed-penalty, since you will read the full file into memory when you do File.readlines()

In exercise 16 he doesn't use it and just keeps going into writing.

Nobody says it is a good tutorial. :)

I prefer the old Chris Pine one, but that should be updated to make-fit for 2015.

I wrote a short script to read a text file containing a number, then '.to_i' that number and return a boolean value. I know I'm missing something, and it doesn't add up to me.

You use .to_i to get an Integer.

Like:

"5".to_i # 5

You will get back 5 and not "5".

Your statement of "returning a boolean value" does not seem to make a lot of sense, because there are only two "boolean" values in ruby, which are true and false. You can use other variables too, for if/else checks, such as nil, which is somewhat similar to false (but not identical):

unless nil
  puts 'Hello World!'
end

Basically all I need to know is how to assign the txt.read output to a new variable (simply called, 'variable') and then use gets.chomp.to_i to convert it to an integer so the boolean comparison works. Any help is appreciated, thank you!

Yes, just assign it.

filename = gets.chomp

^^^ Here you will have the input in the variable called filename.

txt = open(filename)

^^^ This is not good. Why not use File.open() instead?

print txt.read

I don't like this. You should use .read() on the method above like:

txt = File.read(filename)

No need for .open().

variable = txt.read.to_i ##Trying to assign the txt.read to a variable?

This makes no sense. Why do you use .to_i?

You don't need an Integer/Fixnum there. You need a String.

print { variable > 4 && false }

This line also makes no sense to me.

You seem to want to do some check based on a number there?