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:
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
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:
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 }
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:
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
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
On Jan 21, 11:23 pm, Glenn Jackman <gle...@ncf.ca> wrote:
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:
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:
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
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
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: