Reading files

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)

···

--
Stefan

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)
  

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

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
sally@yahoo.com

data = IO.readlines("data.txt")

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

--
Posted via http://www.ruby-forum.com/\.

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

···

On 10/9/07, Justin Collins <justincollins@ucla.edu> wrote:

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)
>
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

--
Stefan

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:

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

On 10/9/07, Justin Collins <justincollins@ucla.edu> wrote:

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)

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

--
Stefan

Alex Gutteridge

Bioinformatics Center
Kyoto University

ah yes...
thanks Alex

···

On 10/9/07, Alex Gutteridge <alexg@kuicr.kyoto-u.ac.jp> wrote:

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:

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

>
> On 10/9/07, Justin Collins <justincollins@ucla.edu> wrote:
>>
>> 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)
>>>
>> 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
>>
>>
>
>
> --
> Stefan

Alex Gutteridge

Bioinformatics Center
Kyoto University

--
Stefan