From: James Edward Gray II Date: 2005-10-12T11:32:22+09:00 Subject: Re: Ruby noob with a coming from Python question On Oct 11, 2005, at 7:06 PM, Kevin Ballard wrote: > > James Edward Gray II wrote: > >> On Oct 11, 2005, at 3:16 PM, ES wrote: >> >> Pretty much the same thing, but this should allow you to leave of >> some strips later, I hope: >> >> def hash_from_file( file_name ) >> Hash[*File.read(file_name).split("\n").map { |line| line.split >> ("=") }.flatten] >> end >> >> James Edward Gray II >> > > Erm, what do you mean "to leave of some strips"? The only difference I > can see is readlines(file) -> read(file_name).split("\n"), and I'm not > sure what the difference is. Let's ask Ruby: Neo:~/Desktop$ cat multiline_data.rb #!/usr/local/bin/ruby -w data = DATA.pos p DATA.readlines DATA.seek data p DATA.read.split("\n") __END__ Line one. Line two. Line three. Neo:~/Desktop$ ruby multiline_data.rb ["Line one.\n", "Line two.\n", "Line three.\n"] ["Line one.", "Line two.", "Line three."] If you go back and look at the original code now, I was hoping that would save all the random calls to strip() when Hash data is accessed, thought the "=" split() pattern may also need to become / \s*=\s*/ to get leading whitespace. James Edward Gray II