printing on new lines

  • Thread starter Johnathan Smith
  • Start date
J

Johnathan Smith

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
 
M

MonkeeSage

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

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
 
P

Phrogz

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,200
Latest member
LaraHunley

Latest Threads

Top