# Printing on new lines

**URL:** <https://www.ruby-forum.com/t/printing-on-new-lines/122417>\
**Category:** Ruby\
**Created:** [December 7, 2007, 1:18pm UTC](https://www.ruby-forum.com/t/printing-on-new-lines/122417 "2007-12-07T13:18:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d9b06d/32.png) [@johnsmith](https://www.ruby-forum.com/u/johnsmith)\
**Post date:** [December 7, 2007, 1:18pm UTC](https://www.ruby-forum.com/t/printing-on-new-lines/122417/1 "2007-12-07T13:18:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![johnsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d9b06d/32.png) [@johnsmith](https://www.ruby-forum.com/u/johnsmith)\
**Post date:** [December 7, 2007, 2:32pm UTC](https://www.ruby-forum.com/t/printing-on-new-lines/122417/2 "2007-12-07T14:32:52Z")

</div>

On Dec 7, 6:18 am, Johnathan S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/Hash.html)  
[http://www.ruby-doc.org/core/classes/Array.html](http://www.ruby-doc.org/core/classes/Array.html)

Regards,  
Jordan

---

<div class="post-metadata">

**Author:** ![johnsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d9b06d/32.png) [@johnsmith](https://www.ruby-forum.com/u/johnsmith)\
**Post date:** [December 7, 2007, 3:41pm UTC](https://www.ruby-forum.com/t/printing-on-new-lines/122417/3 "2007-12-07T15:41:02Z")

</div>

On Dec 7, 5:18 am, Johnathan S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
