How do you limit the line length of the output commands? Where is pqueue library documented?

There must be an easy way to solve the problem of controlling the length
of
the output line, forcing the rest into a line break. In the puts code
example below, I want to break between every 10 methods or just go to a
new
line after every 60 characters that get displayed so they don’t run off
the
screen.

First Question: What is an easy way to modify the below puts code to
control
line length without truncation?

require “pqueue”
pq=PQueue.new(proc{|x,y| x[0][0]<y[0][0]})
puts pq.methods.join(’, ') # Displays the methods of PQueue Class

I’ve a tried puts, print, and p and they never do a word wrap or such.
Is
there a secret parameter for controlling the line length before word
wrap
occurs in these commands?

Second question: The pqueue library is not documented in
http://www.ruby-doc.org/stdlib/ http://www.ruby-doc.org/. Is there a
reason for that? Or is it cleverly hidden in a class that doesn’t seem
to
have anything to do with priority queues?

Thanks once more to the Ruby masters,

No Sam

On Sep 23, 2009, at 4:59 PM, Mason K. wrote:

There must be an easy way to solve the problem of controlling the
length of
the output line, forcing the rest into a line break.

There’s not really a built-in tool for this, no.

require “pqueue”
pq=PQueue.new(proc{|x,y| x[0][0]<y[0][0]})
puts pq.methods.join(’, ') # Displays the methods of PQueue Class

Try this:

require “pp”
pp pg.methods

I’ve a tried puts, print, and p and they never do a word wrap or
such. Is
there a secret parameter for controlling the line length before word
wrap
occurs in these commands?

The pp for “pretty print” library does account for things such as
wrapping.

Second question: The pqueue library is not documented in
http://www.ruby-doc.org/stdlib/ http://www.ruby-doc.org/. Is
there a
reason for that?

The reason would be that it’s not a standard library that ships with
Ruby. You must have grabbed it from somewhere, so that’s where you
should look for documentation.

Hope that helps.

James Edward G. II

On 9/24/09 12:59 AM, Mason K. wrote:

First Question: What is an easy way to modify the below puts code to control
line length without truncation?

require “pqueue”
pq=PQueue.new(proc{|x,y| x[0][0]<y[0][0]})
puts pq.methods.join(’, ') # Displays the methods of PQueue Class

I’ve a tried puts, print, and p and they never do a word wrap or such. Is
there a secret parameter for controlling the line length before word wrap
occurs in these commands?

PP::pp has a width parameter, but in this case I think it’ll print one
method per line (not say 10 per line) if you did PP::pp pq.methods.

If that doesn’t satisfy you I’d guess you’re stuck with stuffing the
methods into the end of a buffer yourself. (sorry this feels more like C
than ruby to this ruby newbie but I can’t think of a simpler way)

buffer = ‘’
pq.methods.each do |m|
if buffer.split("\n")[-1].to_s.length + m.length < 60
buffer << ((buffer.length > 0)? “, #{m}”: “#{m}”)
else
buffer << “\n#{m}”
end
end

puts buffer

Second question: The pqueue library is not documented in
http://www.ruby-doc.org/stdlib/http://www.ruby-doc.org/. Is there a
reason for that? Or is it cleverly hidden in a class that doesn’t seem to
have anything to do with priority queues?

I’m not sure how you got pqueue but in my case (to test this example) I
had to install it as a gem i.e it’s not distributed with my ruby
(1.8.7).

gem query -d -n pqueue gives http://death.rubyforge.org/ as the homepage
so that’s where I’d start looking.

On Wednesday 23 September 2009 04:59:23 pm Mason K. wrote:

In the puts code
example below, I want to break between every 10 methods or just go to a new
line after every 60 characters that get displayed so they don’t run off the
screen.

It sounds like you’re viewing your output in something wacky, like
Notepad.
Terminals usually wrap things, and so do good text editors – they’ll
just
wrap by character, not by word or method.

I don’t know that there’s an “easy way” as in “built into the language”,
but
there’s probably a library to at least wrap on word. After all, it’s not
really a property of the IO itself, it’s more string manipulation.

Is
there a secret parameter for controlling the line length before word wrap
occurs in these commands?

Word wrap does not occur in these commands. If you’re seeing
word-wrapping at
all, again, that’s likely a text editor or a terminal doing it for you.

If it’s a terminal, I’m really not sure what you’re complaining about –
just
resize the terminal window.

Second question: The pqueue library is not documented in
http://www.ruby-doc.org/stdlib/ http://www.ruby-doc.org/.

Yeah, I have no idea where you got it from. I can’t find it in my
standard
library for 1.8 or 1.9, or in

Are you sure you didn’t install a gem or something?

No. I haven’t gotten around to learning GEMs yet or downloaded any
special
libraries. I’m just now becoming aware of the classes that come
with1.8,
the version I down loaded two months ago. Have not imported any Gems
that I
am aware of. Not ready for Ruby 1.9 yet.

I am running my Ruby code on SciTE, a free IDE that came with the
download.
But that shouldn’t be an issue here as what I want is a code way of
controlling the format of my display. I just have to have the code in a
particular subdirectory in order for the retrieve to work.

I tried the code:
retrieve pp
pp pq.methods

and it produce the names of all of the methods for the priority queue,
one
on each line. Better than just one line but not good enough.

I did the same thing for
pp pp.methods
and got the methods of pp. I noticed that many were shared by pq,
making me
think that they inherited from a super class that they both must belong
to.

I tried
pp(pq.methods, out=$>, width=60)
as was suggested in the Ruby standard documentation for pretty print.
But
that just gives me errors, prints out one method per line and the number
60
at the end. I’m not going something right.

The code that Patrick suggested works best:
buffer = ‘’
pq.methods.each do |m|
if buffer.split("\n")[-1].to_s.length + m.length < 60
buffer << ((buffer.length > 0)? “, #{m}”: “#{m}”)
else
buffer << “\n#{m}”
end
end
puts buffer

It is very interesting as it prints out five or six methods per line,
staying under 60 character length. So it gives a much more readable
format. Still, Ruby needs a simple formatter for its display output
class.
But at least I can stick this in a method and use it in the future.

Much thanks to everyone for all the useful knowledge, and special thanks
to
Patrick for the extra mile. Slowly but surely I am learning.

No Sam

On 9/23/09, Patrick O. [email protected] wrote:

On 9/24/09 12:59 AM, Mason K. wrote:

First Question: What is an easy way to modify the below puts code to
control
line length without truncation?
[snip]
end
end

puts buffer

There’s a way to do this using Regexp and gsub, but I almost always
mess it up when I try to code it off the top of my head. It seems to
be difficult enough that its actually better to do it like you have
above, even tho the regexp version is only like 2 lines.

Caleb C. wrote:

If that doesn’t satisfy you I’d guess you’re stuck with stuffing the
end

puts buffer

There’s a way to do this using Regexp and gsub, but I almost always
mess it up when I try to code it off the top of my head. It seems to
be difficult enough that its actually better to do it like you have
above, even tho the regexp version is only like 2 lines

This is a pretty decent one I’ve been using:

class String
def word_wrap length
self.gsub(/([^\r\n\n\s\Z]{#{length}})/, "\1
").gsub(/(.{1,#{length}})(\r\n|\n|\s+|\Z)/, “\1\n”)
end
end

puts pq.methods.join(", ").word_wrap

-Justin

Oh! This less code to write even if it leans heavily on knowing how to
do
regular expressions, which I am still learning. Very nice.

But you made a minor typo error in your command
puts pq.methods.join(", “).word_wrap
which should have been
puts pq.methods.join(”, ").word_wrap(50)
where the 50 is the max. length of the line.

Much thanks, Good Code! I tested it for several different lengths.

No Sam

[snip]
else
be difficult enough that its actually better to do it like you have
end

No Sam

Yes, sorry. Was going kind of fast. There is also an extra ‘\n’ in the
first regex.

-Justin

On Wednesday 23 September 2009 09:07:16 pm Mason K. wrote:

I am running my Ruby code on SciTE, a free IDE that came with the download.

There you go. If you run Ruby outside of SciTE, does that class exist?

I tried the code:
retrieve pp

What does this do? I’m not aware of a ‘retrieve’ method in Ruby. Maybe
you
meant something like this:

require ‘pp’

and it produce the names of all of the methods for the priority queue, one
on each line. Better than just one line but not good enough.

What I’d assumed, but can’t find decent documentation for, is that pp
can
customize the output somewhat.

I did the same thing for
pp pp.methods
and got the methods of pp.

When you run pp like this:

pp foo.methods

That expands to this:

pp(foo.methods)

So what you’re really doing is trying to find:

pp().methods

Does that make any sense?

In other words, you’re treating pp like an object, which it’s not.
You’re
actually calling ‘methods’ on the return value of the method pp, which
returns nil for me. In other words, what you’re doing is probably
equivalent
to looking at nil.methods.

I noticed that many were shared by pq, making
me think that they inherited from a super class that they both must belong
to.

Probably true. It’s not trivial to find out whether a method is
inherited from
within Ruby. I’ve been doing things like this, from the commandline:

ri pp

That, and otherwise looking at methods.

I tried
pp(pq.methods, out=$>, width=60)

Giant, gaping syntax error: Ruby doesn’t really have keyword arguments.
That’s
showing what the argument is called, and what its default value is – in
other
words, it’s showing how you might define it. So, you probably wanted:

PP.pp(pq.methods, $> 60)

This still won’t do what you want, though – it works well enough for
arrays
that fit on one line, but as soon as the array spans multiple lines, it
shows
one per line.

And yes, reading the source, you do have to call PP.pp if you want to
use
those arguments. The normal ‘pp’ is a wrapper that just calls PP.pp on
each of
its arguments, which is why you got a ‘60’ at the end.

It is very interesting as it prints out five or six methods per line,
staying under 60 character length.

Sounds like exactly what you wanted. So what’s the problem?

Still, Ruby needs a simple formatter for its display output class.

What “display output class”? And why?

But at least I can stick this in a method and use it in the future.

Exactly.

Why should this be part of the language, or even the standard library?
Ruby is
used in all sorts of places. It seems to be quite popular on the web,
for
example, where output is to a web browser which can clearly word-wrap
for you.
I can’t remember ever wanting this feature.

So, if it’s useful to you, by all means, put it in a function, or find a
library that serves your purpose. Or write one, make a gem, and share it
with
the world.

But what you’re suggesting makes no sense. Ruby doesn’t have a “display
output
class”. It has IO classes, which may or may not have anything to do with
display. It also has bindings to various GUI and network libraries,
which
might be what it’s using for display instead.

I would suggest it’s not really Ruby’s job, nor is it necessarily even
the job
of the program you’re writing. If you’re on any sort of Unix, I’d
suggest
piping the output through the ‘fold’ command.

If you’re outputting to anything other than the terminal, there’s a good
chance that tool will support word-wrapping – which is why I asked if
you
were using something like Notepad to view the output. Surely SCiTE
should have
some option to wrap long lines for you, if not on individual words.

On Sep 23, 5:59 pm, Mason K. [email protected] wrote:

pq=PQueue.new(proc{|x,y| x[0][0]<y[0][0]})
puts pq.methods.join(’, ') # Displays the methods of PQueue Class

I’ve a tried puts, print, and p and they never do a word wrap or such. Is
there a secret parameter for controlling the line length before word wrap
occurs in these commands?

Second question: The pqueue library is not documented inhttp://www.ruby-doc.org/stdlib/http://www.ruby-doc.org/. Is there a
reason for that? Or is it cleverly hidden in a class that doesn’t seem to
have anything to do with priority queues?

I’ve been transitioning to GitHub, and thanks to your question, this
morning I got the Grancher plugin for my build system running, so now
you can find it here:

http://rubyworks.github.com/pqueue/

Thanks,
T.

Thanks for the link to documentation on PQueue. If you maintain the
link
you might want to make it clear in the description that you can order
ascending OR descending in the priority queue, and that any component
(subarray) of the array can be used to sort/prioritize on. This
automatic
feature is of use in a lot of different applications.

In my code I am adding elements in an ascending order on the first
subarray,
the heuristic value created by another method. My code is:
pq=PQueue.new(proc{|x,y| x[0][0]*<y[0][0]})
but if I had wanted descending order I would have written
pq=PQueue.new(proc{|x,y| x[0][0]
>*y[0][0]}). Note the change from “<”
to
“>”. And if I had wanted to sort on different subarray I would have
changed
the values in the brackets accordingly, just making sure that the x and
y
values were the same. Would be nice to have minor sort capabilities too
in
some applications.

I also recommend that all Ruby documentation include simple real code
examples of their use by including actual code that has been tested,
with
all the essential components included in the example.

No Sam

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs