[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

printing on new lines

Johnathan Smith

12/7/2007 12:19:00 PM

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
--
Posted via http://www.ruby-....

7 Answers

MonkeeSage

12/7/2007 1:30:00 PM

0

On Dec 7, 6:18 am, Johnathan Smith <stu...@hotmail.com> wrote:
> 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
> --
> Posted viahttp://www.ruby-....

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...
http://www.ruby-doc.org/core/classes/...

Regards,
Jordan

Phrogz

12/7/2007 2:37:00 PM

0

On Dec 7, 5:18 am, Johnathan Smith <stu...@hotmail.com> 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

rst9

2/27/2012 4:58:00 PM

0

On Feb 27, 5:45 am, baldeagle <force....@gmail.com> wrote:
> On Feb 27, 3:04 pm, rst9 <rst9w...@yahoo.com> wrote:
> > The word is "regularity".  Keep the same schedule every day.
> > Example:  When your work schedule is 8 - 5, go to bed by 10 pm, and
> > get up every day by 7 am, and keep this schedule everyday
> > including weekends.  On nights when you can't sleep, get
> > up at 7 am anyway, and don't take a nap in the daytime.
> > Even when you can't sleep two nights in a row, get up at
> > 7 am.  After one night without sleep, you'll be so
> > tired you fall asleep as soon as you get to bed.  Of course,
> > after two nights without sleep, you'll be asleep before you
> > hit the bed.
>
> > Never take pills to sleep.  Never, never stay in bed
> > all day trying to get some sleep.
>
> Yes. Never, never take pills or other drugs to artificially induce
> sleep.  Michael Jackson was a victim.
>
> Most people, believe that our sleep must not be interrupted...

Not true. Sleep is never continuous. You wake up many times during
the night.

> we should  be able to sleep for 8 hours without a break.

This is never true.

> If we wake up in the middle of the night, we panic

Normally, you just roll over and continue to sleep, many times you
don't even remember.

> ...thinking
> there is something wrong with us...

You think too much.

> Some would go to the
> doctor...to get help...

Doctor can't help you. You gotta help yourself.

> this is the start on the road to drug
> dependency.

Yes, doctor can only prescript drugs.

>
> Now scientists have found out that it is perfectly normal
> for many of us to break our sleep into two periods...about
> 3 to 4 hours each.

More likely many periods.

> In between sleep, we could get up and do other things...like
> reading a book,..catch up with your email.. surf the internet...
> chat with your spouse...or make love.
>
> If I wake up in the middle of the night,...I have a routine...
> have a snack (comfort food)...

Bad habit. You get fat fast.

> watch headline news (repeated
> many times during the day)...or read a boring meaningless
> book..like the Sutra..the bible...or the Quran...or make love.

Or get dress and start your day early.


baldeagle

2/27/2012 11:36:00 PM

0

On Feb 28, 12:57 am, rst9 <rst9w...@yahoo.com> wrote:
> On Feb 27, 5:45 am, baldeagle <force....@gmail.com> wrote:
>
>
..> > If I wake up in the middle of the night,...I have a
..> > routine... have a snack (comfort food)...
>
> Bad habit.  You get fat fast.
>
True... In the morning...I always get the feeling... I have
put on another 2 kilos. But I feel ok, have lost the weight
gain...after my daily jog around the reservoir. My weight
has been 80 kg or below ..for the last 20 years.

> > watch headline news (repeated
> > many times during the day)...or read a boring meaningless
> > book..like the Sutra..the bible...or the Quran...or make love.
>
> Or get dress and start your day early.

Occasionally I got too carried away..with the breaking news
I did not return to bed. But I catch up with a nap in the afternoon.

rst0wxyz

2/27/2012 11:52:00 PM

0

On Feb 27, 3:35 pm, baldeagle <force....@gmail.com> wrote:
> On Feb 28, 12:57 am, rst9 <rst9w...@yahoo.com> wrote:> On Feb 27, 5:45 am, baldeagle <force....@gmail.com> wrote:
>
> .> > If I wake up in the middle of the night,...I have a
> .> > routine... have a snack (comfort food)...
>
> > Bad habit.  You get fat fast.
>
> True... In the morning...I always get the feeling... I have
> put on another 2 kilos.  But I feel ok, have lost the weight
> gain...after my daily jog around the reservoir. My weight
> has been 80 kg or below ..for the last 20 years.

80 kg is about 175 pounds which is about my son's weight. My son is 6
feet tall. How tall are you?

>
> > > watch headline news (repeated
> > > many times during the day)...or read a boring meaningless
> > > book..like the Sutra..the bible...or the Quran...or make love.
>
> > Or get dress and start your day early.
>
> Occasionally I got too carried away..with the breaking news
> I did not return to bed. But I catch up with a nap in the afternoon.

Now here is the cause of your sleeplessness, a nap in the afternoon
which is the cause of your sleep,essness at night.


baldeagle

2/28/2012 12:09:00 AM

0

On Feb 28, 7:52 am, rst0 <rst0w...@yahoo.com> wrote:
> On Feb 27, 3:35 pm, baldeagle <force....@gmail.com> wrote:
>
> > On Feb 28, 12:57 am, rst9 <rst9w...@yahoo.com> wrote:> On Feb 27, 5:45 am, baldeagle <force....@gmail.com> wrote:
>
> > .> > If I wake up in the middle of the night,...I have a
> > .> > routine... have a snack (comfort food)...
>
> > > Bad habit.  You get fat fast.
>
> > True... In the morning...I always get the feeling... I have
> > put on another 2 kilos.  But I feel ok, have lost the weight
> > gain...after my daily jog around the reservoir. My weight
> > has been 80 kg or below ..for the last 20 years.
>
> 80 kg is about 175 pounds which is about my son's weight.  My son is 6
> feet tall.  How tall are you?

I was 5 -10 but I think..I have shrunk a little..all the
young kids seem to be taller and bigger than I am.

>
>
>
> > > > watch headline news (repeated
> > > > many times during the day)...or read a boring meaningless
> > > > book..like the Sutra..the bible...or the Quran...or make love.
>
> > > Or get dress and start your day early.
>
> > Occasionally I got too carried away..with the breaking news
> > I did not return to bed. But I catch up with a nap in the afternoon.
>
> Now here is the cause of your sleeplessness, a nap in the afternoon
> which is the cause of your sleep,essness at night.

I've got the habit of afternoon nap...when I visited
Arizona for about 3 months during the summer a
few years back. It was over 100 degree...every
one stayed indoor in airconditioned rooms... For
me, I had an afternoon nap after lunch. I enjoyed
the nap...and doctor say...it is good for me.

rst0wxyz

2/28/2012 3:27:00 AM

0

On Feb 27, 4:08 pm, baldeagle <force....@gmail.com> wrote:
> On Feb 28, 7:52 am, rst0 <rst0w...@yahoo.com> wrote:
> > On Feb 27, 3:35 pm, baldeagle <force....@gmail.com> wrote:
>
> > 80 kg is about 175 pounds which is about my son's weight.  My son is 6
> > feet tall.  How tall are you?
>
> I was 5 -10  but I think..I have shrunk a little..all the
> young kids seem to be taller and bigger than I am.

Today's young people are taller. They have better food.

> > Now here is the cause of your sleeplessness, a nap in the afternoon
> > which is the cause of your sleep,essness at night.
>
> I've got the habit of afternoon nap...when I visited
> Arizona for about 3 months during the summer a
> few years back.  It was over 100 degree...every
> one stayed indoor in airconditioned rooms... For
> me, I had an afternoon nap after lunch. I enjoyed
> the nap...and doctor say...it is good for me.

I lived in Arizona for 11 years, from 1949 - 1960. I considered
Arizona my home until my parents died. The last time I visited
Arizona when in 2005 when John Venning invited me to his concert in
Tucson. So much had changed, I can't even recognized the University
of Arizona.