# An elegant way to modify all lines from file by using map() function?

**URL:** <https://www.ruby-forum.com/t/an-elegant-way-to-modify-all-lines-from-file-by-using-map-function/182044>\
**Category:** Ruby\
**Created:** [January 21, 2010, 11:20pm UTC](https://www.ruby-forum.com/t/an-elegant-way-to-modify-all-lines-from-file-by-using-map-function/182044 "2010-01-21T23:20:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![RobertGawron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/49beb7/32.png) [@RobertGawron](https://www.ruby-forum.com/u/RobertGawron)\
**Post date:** [January 21, 2010, 11:20pm UTC](https://www.ruby-forum.com/t/an-elegant-way-to-modify-all-lines-from-file-by-using-map-function/182044/1 "2010-01-21T23:20:27Z")

</div>

Hi all,

I’m new in ruby and I just have a question: in python when I want to  
read all lines from file, do something with all of them and then use  
them in my program I use map function + lambda function, sth like  
that:

print map(lambda u: u.upper(), open(“foo”, “r”).readlines())

but in your language I didn’t find the way to create in one step array  
of all lines from file, that could be used by map(). If I really want  
to use map in this situation I have to use secondary array (called  
arr) like in this snippet?

arr = Array.new  
File.open(‘foo’).each\_line{ |l| arr.push l }  
p arr.collect{ |l| l.upcase }

Please post a code-snippet, it’s quite interesting to me 🙂

---

<div class="post-metadata">

**Author:** ![RobertGawron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/49beb7/32.png) [@RobertGawron](https://www.ruby-forum.com/u/RobertGawron)\
**Post date:** [January 21, 2010, 11:29pm UTC](https://www.ruby-forum.com/t/an-elegant-way-to-modify-all-lines-from-file-by-using-map-function/182044/2 "2010-01-21T23:29:14Z")

</div>

At 2010-01-21 05:17PM, “RobertGawron” wrote:

> I’m new in ruby and I just have a question: in python when I want to  
> read all lines from file, do something with all of them and then use  
> them in my program I use map function + lambda function, sth like  
> that:
> 
> print map(lambda u: u.upper(), open(“foo”, “r”).readlines())

puts File.readlines(“foo”).map {|l| l.upcase}

---

<div class="post-metadata">

**Author:** ![RobertGawron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/49beb7/32.png) [@RobertGawron](https://www.ruby-forum.com/u/RobertGawron)\
**Post date:** [January 21, 2010, 11:34pm UTC](https://www.ruby-forum.com/t/an-elegant-way-to-modify-all-lines-from-file-by-using-map-function/182044/3 "2010-01-21T23:34:31Z")

</div>

Thanks

---

<div class="post-metadata">

**Author:** ![RobertGawron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/49beb7/32.png) [@RobertGawron](https://www.ruby-forum.com/u/RobertGawron)\
**Post date:** [January 21, 2010, 11:32pm UTC](https://www.ruby-forum.com/t/an-elegant-way-to-modify-all-lines-from-file-by-using-map-function/182044/4 "2010-01-21T23:32:45Z")

</div>

On Thu, Jan 21, 2010 at 4:20 PM, RobertGawron [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> but in your language I didn’t find the way to create in one step array  
> of all lines from file, that could be used by map(). If I really want  
> to use map in this situation I have to use secondary array (called  
> arr) like in this snippet?
> 
> arr = Array.new  
> File.open(‘foo’).each\_line{ |l| arr.push l }  
> p arr.collect{ |l| l.upcase }

On 1.8.7+, you can use .lines to get an Enumerator:

p File.open(‘foo’).lines.map {|l| l.upcase}

- Charlie

---

<div class="post-metadata">

**Author:** ![RobertGawron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/49beb7/32.png) [@RobertGawron](https://www.ruby-forum.com/u/RobertGawron)\
**Post date:** [January 21, 2010, 11:40pm UTC](https://www.ruby-forum.com/t/an-elegant-way-to-modify-all-lines-from-file-by-using-map-function/182044/5 "2010-01-21T23:40:57Z")

</div>

Hi,

2010/1/21 RobertGawron [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> of all lines from file, that could be used by map(). If I really want  
> to use map in this situation I have to use secondary array (called  
> arr) like in this snippet?
> 
> arr = Array.new  
> File.open(‘foo’).each\_line{ |l| arr.push l }  
> p arr.collect{ |l| l.upcase }
> 
> Please post a code-snippet, it’s quite interesting to me 🙂

Waow awesome how the order is in Python…

Let’s take the natural order, welcome to Ruby !

puts File.read(‘foo’).lines.map { |line| line.upcase }  
That’s in ruby 1.9  
Notice lines is an Enumerator, that can be converted to an Array if  
needed  
by #to\_a

You got also IO.readlines

---

<div class="post-metadata">

**Author:** ![RobertGawron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/49beb7/32.png) [@RobertGawron](https://www.ruby-forum.com/u/RobertGawron)\
**Post date:** [January 21, 2010, 11:55pm UTC](https://www.ruby-forum.com/t/an-elegant-way-to-modify-all-lines-from-file-by-using-map-function/182044/6 "2010-01-21T23:55:35Z")

</div>

On Jan 21, 2010, at 5:40 PM, Benoit D. wrote:

> > p arr.collect{ |l| l.upcase }  
> > Notice lines is an Enumerator, that can be converted to an Array if  
> > needed  
> > by #to\_a
> 
> You got also IO.readlines

Don’t stop there! Take advantage of the Symbol#to\_proc behavior:

puts File.read(‘foo’).lines.map(&:upcase)

😉

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![RobertGawron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/49beb7/32.png) [@RobertGawron](https://www.ruby-forum.com/u/RobertGawron)\
**Post date:** [January 22, 2010, 9:39am UTC](https://www.ruby-forum.com/t/an-elegant-way-to-modify-all-lines-from-file-by-using-map-function/182044/7 "2010-01-22T09:39:08Z")

</div>

2010/1/21 Charles Oliver N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> On 1.8.7+, you can use .lines to get an Enumerator:
> 
> p File.open(‘foo’).lines.map {|l| l.upcase}

This does not close the file handle properly.

I’d rather do something like this:

File.foreach(‘foo’).map {|l| l.upcase}

File.foreach(‘foo’).map(&:upcase)

These have the advantage over some of the presented solutions that  
they do not create two large Arrays in memory but just one - the  
mapped version.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![RobertGawron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/49beb7/32.png) [@RobertGawron](https://www.ruby-forum.com/u/RobertGawron)\
**Post date:** [January 22, 2010, 3:55pm UTC](https://www.ruby-forum.com/t/an-elegant-way-to-modify-all-lines-from-file-by-using-map-function/182044/8 "2010-01-22T15:55:31Z")

</div>

Thanks for all replies.
