Console output formatting

Hello all, I got two quick questions. First can anyone tell me how to
format the output of a hash in pairs in a table format like

a=1 b=2 c=3
d=4 e=5 f=6

I would prefer the list go down the left most row first but across would
be fine.

Second is there any tutorials or gems/librarys that will deal with
console formating on a screen scale rather than strings. I am thinking
the way aircrack-ng looks. I am not trying anything illegal, just one of
the few programs that I know look like what I want. A picture is at
http://www.aircrack-ng.org/screenshots/aircrack-ng.gif .

Thanx in advance.

Try,

hash.each {|key,val| printf “%s = %d\n”,key,val}

or

hash.each_with_index do |a,i|
printf "%s = %d ",a.first,a.last
puts if (i%3==2)
end

On Tue, Feb 22, 2011 at 6:53 AM, ra don [email protected] wrote:

Hello all, I got two quick questions. First can anyone tell me how to
format the output of a hash in pairs in a table format like

a=1 b=2 c=3
d=4 e=5 f=6

ruby is an enjoyable language to work or play with.

eg, try each_slice,

h #=> {:a=>1, :b=>2, :c=>3, :d=>4, :e=>5, :f=>6}

h.each_slice(3) do |x,y,z|
p x+y+z
end

[:a, 1, :b, 2, :c, 3]
[:d, 4, :e, 5, :f, 6]

Second is there any tutorials or gems/librarys that will deal with
console formating on a screen scale rather than strings…

search about curses. read more ruby docs/manuals and try the tutorials…

best regards -botp

You want RubyDL

stephen

RubyDL looks like it would work but can’t find any good docs. Here is
what I got down.

require ‘dl’
me = “”
clib = DL.dlopen(“libc.so.6”)
message = “Hello\n”
printf(“Testing: %s”, message)
clib.scanf("%s", me)
printf(“danrn, %s!\n”, me)
puts message

running in irb i get no errors loading but the printf throws an error if
I put clib. in front of it. The scanf gets throws an error also. I know
I am doing something incorrectly but I don’t know what. Thanks for your
help and info.

Thank you guys that is perfect. I got one more quick question off
topic. Is there an equivalent in ruby to pythons ctype. All research I
have found shows writing code directly for use in ruby(like in the
pickaxe). What I would like to do is make calls directly to c libraries
that aren’t meant for ruby like system libraries.

The ‘dl’ package is certainly one of the most useful Ruby libs out there
and avery clever piece of work. It’s a shame it hasn’t been
documented/supported very well.

The following works on 1.8.6 and I believe it doesn’t in 1.9.
No doubt some gurus will be able to clarify.

require ‘dl’
clib = DL.dlopen("/usr/lib/libc.sl")
strlen=clib[‘strlen’,“IS”]
message = “Hello\n”
p strlen[message].first => 6

The above is calling strlen which has simple parameters,
an 'I’nteger return, and a 'S’tring by reference.
(It’s too late in the night to do scanf, and the Standard library
already does scanf)

You can use this for any C routine, as long as you get the
parameter prototype string setup correctly.

Oops. I was using 1.9 and guess I didn’t mention that. I will try the
above in 1.8 and see if I get any further. Thanks a lot.

Hi.
If you look in your ruby folders, you should find a file called dl.txt.
Rudimentary and cryptic documuntation, but reading it with a bit of
patience may help. It is some time ago that I looked it, and the best
doc I found was at http://www.jbrowse.com/text/rdl_en.html but at the
moment I get ‘access denied’ when I tried to go there.

The method I described earlier has served me very well and it is quite
easy once you get the hang of it.
Firtly, you tell DL where your library is :
lib1=DL.dlopen(library) # Open library
then you create ‘pointers’ to the routines you want, along with the
parameter prototype string. I remember there were quite a few parameter
code, but the only ones I ended up using were ‘I’ & ‘S’ without
problems.
Some others were “L” for long, “P” for pointer.
As long as you are clear about passing parameters by value or by
reference you should be ok. (I don’t do C, but I still manage)

func1=lib1[‘funtion1’,‘ISSSS’) # integer returncode +4 parameters passed
by reference
Then to call the function, you can use .call or []

returncode=func1.call(p1,p2,p3,“p4”).first
or
returncode=func1[p1,p2,p3,“p4”].first

HTH

ra don wrote in post #982995:

Hello all, I got two quick questions. First can anyone tell me how to
format the output of a hash in pairs in a table format like

a=1 b=2 c=3
d=4 e=5 f=6

That’s a good question.

PHP has the “Console Table” extension:

http://pear.php.net/manual/en/package.console.console-table.introduction.php

(It also supports colors.) I wonder if Ruby has something similar.
People here didn’t quite answer this question.

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