Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

0 views
Skip to first unread message

RobertGawron

unread,
Jan 21, 2010, 5:17:46 PM1/21/10
to
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 :)

Glenn Jackman

unread,
Jan 21, 2010, 5:23:46 PM1/21/10
to
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}


--
Glenn Jackman
Write a wise saying and your name will live forever. -- Anonymous

RobertGawron

unread,
Jan 21, 2010, 5:25:55 PM1/21/10
to
Thanks

Charles Oliver Nutter

unread,
Jan 21, 2010, 5:29:11 PM1/21/10
to
On Thu, Jan 21, 2010 at 4:20 PM, RobertGawron <mojwp...@gmail.com> 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

Benoit Daloze

unread,
Jan 21, 2010, 5:40:29 PM1/21/10
to
[Note: parts of this message were removed to make it a legal post.]

Hi,

2010/1/21 RobertGawron <mojwp...@gmail.com>

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

Rob Biedenharn

unread,
Jan 21, 2010, 5:54:22 PM1/21/10
to


Don't stop there! Take advantage of the Symbol#to_proc behavior:

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

;-)

-Rob

Rob Biedenharn http://agileconsultingllc.com
R...@AgileConsultingLLC.com


Robert Klemme

unread,
Jan 22, 2010, 3:33:13 AM1/22/10
to
2010/1/21 Charles Oliver Nutter <hea...@headius.com>:

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

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

RobertGawron

unread,
Jan 22, 2010, 9:53:56 AM1/22/10
to
Thanks for all replies.
0 new messages