# Reading files

**URL:** <https://www.ruby-forum.com/t/reading-files/116821>\
**Category:** Ruby\
**Created:** [October 10, 2007, 2:05am UTC](https://www.ruby-forum.com/t/reading-files/116821 "2007-10-10T02:05:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![smc\_smc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/74df32/32.png) [@smc\_smc](https://www.ruby-forum.com/u/smc_smc)\
**Post date:** [October 10, 2007, 2:05am UTC](https://www.ruby-forum.com/t/reading-files/116821/1 "2007-10-10T02:05:56Z")

</div>

this is just a very basic example but how would you make it so that a  
user  
may put their name (or some other info) into a file and then the program  
reads it and sets it as a variable?

so like:

def say\_hi(who)  
result = "hello " + who  
return result  
end

puts say\_hi(person\_name)

---

<div class="post-metadata">

**Author:** ![smc\_smc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/74df32/32.png) [@smc\_smc](https://www.ruby-forum.com/u/smc_smc)\
**Post date:** [October 10, 2007, 3:26am UTC](https://www.ruby-forum.com/t/reading-files/116821/2 "2007-10-10T03:26:25Z")

</div>

smc smc wrote:

> puts say\_hi(person\_name)

To read a file:

person\_name = File.read(“somefile”)

To read from command line:

person\_name = gets.strip

To make your say\_hi shorter:

def say\_hi(who)  
“hello #{who}”  
end

Hope that helps.

-Justin

---

<div class="post-metadata">

**Author:** ![smc\_smc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/74df32/32.png) [@smc\_smc](https://www.ruby-forum.com/u/smc_smc)\
**Post date:** [October 10, 2007, 5:12am UTC](https://www.ruby-forum.com/t/reading-files/116821/3 "2007-10-10T05:12:30Z")

</div>

smc smc wrote:

> this is just a very basic example but how would you make it so that a  
> user  
> may put their name (or some other info) into a file and then the program  
> reads it and sets it as a variable?
> 
> so like:
> 
> def say\_hi(who)  
> result = "hello " + who  
> return result  
> end
> 
> puts say\_hi(person\_name)

## data.txt

Sally Smith  
123-4567  
removed\_email\_address@domain.invalid

data = IO.readlines(“data.txt”)

puts data[0].strip  
puts data[1].strip  
puts data[2].strip

---

<div class="post-metadata">

**Author:** ![smc\_smc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/74df32/32.png) [@smc\_smc](https://www.ruby-forum.com/u/smc_smc)\
**Post date:** [October 10, 2007, 6:45am UTC](https://www.ruby-forum.com/t/reading-files/116821/4 "2007-10-10T06:45:07Z")

</div>

so how would you set it up?

def say\_hi(who)  
“hello #{who}”  
end  
person\_name = File.read(“name.txt”)  
end

i tried messing around with it a little, it kept giving me syntax errors

---

<div class="post-metadata">

**Author:** ![smc\_smc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/74df32/32.png) [@smc\_smc](https://www.ruby-forum.com/u/smc_smc)\
**Post date:** [October 10, 2007, 7:03am UTC](https://www.ruby-forum.com/t/reading-files/116821/5 "2007-10-10T07:03:54Z")

</div>

The syntax errors are because you have an extra ‘end’. You also don’t  
ever call your method. Is this what you want?

def say\_hi(who)  
“hello #{who}”  
end

person\_name = File.read(“name.txt”)  
puts say\_hi(person\_name)

On 10 Oct 2007, at 12:49, smc smc wrote:

> errors

> > > 
> > 
> > end  
> > Stefan  
> > Alex G.

Bioinformatics Center  
Kyoto University

---

<div class="post-metadata">

**Author:** ![smc\_smc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/74df32/32.png) [@smc\_smc](https://www.ruby-forum.com/u/smc_smc)\
**Post date:** [October 10, 2007, 11:31pm UTC](https://www.ruby-forum.com/t/reading-files/116821/6 "2007-10-10T23:31:53Z")

</div>

ah yes…  
thanks Alex
