# How to read values dynamically

**URL:** <https://www.ruby-forum.com/t/how-to-read-values-dynamically/114451>\
**Category:** Ruby\
**Created:** [September 12, 2007, 8:07pm UTC](https://www.ruby-forum.com/t/how-to-read-values-dynamically/114451 "2007-09-12T20:07:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/13edae/32.png) [@ruby](https://www.ruby-forum.com/u/ruby)\
**Post date:** [September 12, 2007, 8:07pm UTC](https://www.ruby-forum.com/t/how-to-read-values-dynamically/114451/1 "2007-09-12T20:07:33Z")

</div>

In ruby, is there any way to read the values from a fileI am actually  
giving the values in the code itself. Is there a way to read the value  
dynamically

---

<div class="post-metadata">

**Author:** ![ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/13edae/32.png) [@ruby](https://www.ruby-forum.com/u/ruby)\
**Post date:** [September 13, 2007, 7:38am UTC](https://www.ruby-forum.com/t/how-to-read-values-dynamically/114451/2 "2007-09-13T07:38:05Z")

</div>

On Sep 12, 12:07 pm, Rupa R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> In ruby, is there any way to read the values from a fileI am actually  
> giving the values in the code itself. Is there a way to read the value  
> dynamically

Yes, it is possible. You can either read the file into a string and  
parse it to find what you wanted, or you can load the file as ruby  
code (if it is) and have the code run.

If that doesn’t answer your question, please provide more details  
about what you are trying to do.

---

<div class="post-metadata">

**Author:** ![ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/13edae/32.png) [@ruby](https://www.ruby-forum.com/u/ruby)\
**Post date:** [September 13, 2007, 9:17pm UTC](https://www.ruby-forum.com/t/how-to-read-values-dynamically/114451/3 "2007-09-13T21:17:35Z")

</div>

Gavin K. wrote:

> On Sep 12, 12:07 pm, Rupa R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > In ruby, is there any way to read the values from a fileI am actually  
> > giving the values in the code itself. Is there a way to read the value  
> > dynamically
> 
> Yes, it is possible. You can either read the file into a string and  
> parse it to find what you wanted, or you can load the file as ruby  
> code (if it is) and have the code run.
> 
> If that doesn’t answer your question, please provide more details  
> about what you are trying to do.

---

<div class="post-metadata">

**Author:** ![ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/13edae/32.png) [@ruby](https://www.ruby-forum.com/u/ruby)\
**Post date:** [September 14, 2007, 10:49am UTC](https://www.ruby-forum.com/t/how-to-read-values-dynamically/114451/4 "2007-09-14T10:49:35Z")

</div>

Rupa R. wrote:

> In ruby, is there any way to read the values from a fileI am actually  
> giving the values in the code itself. Is there a way to read the value  
> dynamically

f = File.new(“aaa.txt”, “w”)  
f.puts(10)  
f.close

f = File.new(“aaa.txt”, “a”)  
f.puts(20)  
f.close

arr = IO.readlines(“aaa.txt”)  
x = Integer(arr[0])  
y = Integer(arr[1])

puts x+y #30
