Printing on new lines

hello

iv written a class which prints a a number of hashes of refrences

however it all prints on one line. What i was wondering was it is
possible to print each reference on a different line.

Any help would be much appreciated
my code is below

thanks

$linecount = 0

database = {}

opts.each do |opt, arg|
case opt
when ‘–style’
require arg
when ‘–database’

File.open(arg) { | handle |
last_tag = nil

handle.each { | line |

m = line.match(/^(\w+):\s*([\w+,\s]+)$/i)

if m # if m is a match (i.e., not nil)

  if m[1] == 'Tag' # adds a key to the hash

    last_tag = m[2].chomp

    database[last_tag] = {} # makes a subhash as the value

  else

    database[last_tag][m[1]] = m[2].chomp

  end

end

}

}

end

end

print "Number of References: ", database.length, “\n” # prints number
of references

print "Hash Contents: ", database.inspect, “\n” # prints hash contents

On Dec 7, 6:18 am, Johnathan S. [email protected] wrote:

thanks

end

print "Hash Contents: ", database.inspect, “\n” # prints hash contents

Posted viahttp://www.ruby-forum.com/.

Hello Jonathan,

For readability and reusability you should extract the database making
code out of the when block and into its own method (and $linecount is
obsolete now), so…

====

def make_database(filename)
database = {}
File.open(filename) { | handle |
last_tag = nil
handle.each { | line |
m = line.match(/^(\w+):\s*([\w+,\s]+)$/i)
if m # if m is a match (i.e., not nil)
if m[1] == ‘Tag’ # adds a key to the hash
last_tag = m[2].chomp
database[last_tag] = {} # makes a subhash as the value
else
database[last_tag][m[1]] = m[2].chomp
end
end
}
}
return database
end

database = {}

where is opts coming from in this code??

opts.each do |opt, arg|
case opt
when ‘–style’
require arg
when ‘–database’
database = make_database(arg)
end
end

====

Now after #make_database has been called, you can do whatever you like
with the database hash, for example (assuming your previous
reference.txt), to print out a sorted inventory with metadata:

====

this makes an array of arrays, and each

subarray is an array of strings, sorted

on the hash keys, which are composed of

each hash entry and its subhash data

refs = database.keys.sort.map { | title |
subhash = database[title]
[“Reference: #{title}”,
“Type: #{subhash[‘Type’]}”,
“Author: #{subhash[‘Author’]}”]
}

refs now looks like:

#[[“Reference: ref1”,

“Type: Book”,

“Author: Little, S R”],

[“Reference: ref2”,

“Type: Journal”,

“Author: Smith, J”],

[“Reference: ref3”,

“Type: Conference Paper”,

“Author: Williams, M”]]

refs.each_with_index { | ref, i |
puts ref # puts automatically prints each
# item of an array on a new line

print a newline in between refs unless

its the last one

puts if i < refs.length-1
}

====

This outputs:

Reference: ref1
Type: Book
Author: Little, S R

Reference: ref2
Type: Journal
Author: Smith, J

Reference: ref3
Type: Conference Paper
Author: Williams, M

Look at the Hash and Array classes to see exactly what these methods
are doing:

http://www.ruby-doc.org/core/classes/Hash.html
http://www.ruby-doc.org/core/classes/Array.html

Regards,
Jordan

On Dec 7, 5:18 am, Johnathan S. [email protected] wrote:

iv written a class which prints a a number of hashes of refrences

however it all prints on one line. What i was wondering was it is
possible to print each reference on a different line.

require ‘pp’
puts "Hash Contents: ", database.pretty_inspect # prints hash
contents