How to suppress display of specific code in irb??

D

Don Norcott

I am very new to ruby (but a retired experienced C programmer) and am
getting buried in the amount of documentation available. Have been
unable to find an answer to this question, but perhaps do not understand
the concepts involved enough to search correctly. Given this code

require 'hpricot'
url = "http://www....."
doc = Hpricot.XML(open(url))

In interactive ruby can I suppress the output produced by
Hpricot.XML(open(url))

is this the stdout stream or is it something else? At completion the
HTML is in the doc object so is there any need for it to also be
displayed. Perhaps the object has a means of suppressing the data being
loaded?

I am working with web pages and a very large amount of data is involved.
When I mark and copy the irb window to a file to do searches on the data
I have a lot of unneeded and duplicated data. Perhaps there are IRB
parameters to resolve this

I know I can redirect to a file then use the file (but still have same
problem when opening file for processing), but what I would like to do
is the equivalent of redirecting stdout (if it is stdout) to NULL on a
per line basis.

Thanks RustySam
 
J

Justin Collins

I am very new to ruby (but a retired experienced C programmer) and am
getting buried in the amount of documentation available. Have been
unable to find an answer to this question, but perhaps do not understand
the concepts involved enough to search correctly. Given this code

require 'hpricot'
url = "http://www....."
doc = Hpricot.XML(open(url))

In interactive ruby can I suppress the output produced by
Hpricot.XML(open(url))

is this the stdout stream or is it something else? At completion the
HTML is in the doc object so is there any need for it to also be
displayed. Perhaps the object has a means of suppressing the data being
loaded?

I am working with web pages and a very large amount of data is involved.
When I mark and copy the irb window to a file to do searches on the data
I have a lot of unneeded and duplicated data. Perhaps there are IRB
parameters to resolve this

I know I can redirect to a file then use the file (but still have same
problem when opening file for processing), but what I would like to do
is the equivalent of redirecting stdout (if it is stdout) to NULL on a
per line basis.

Thanks RustySam

What you could do is simply append "; nil" to the end of the line:

irb(main):001:0> s = "some really, really long string" ; nil
=> nil


-Justin
 
R

Ryan Davis

In interactive ruby can I suppress the output produced by
Hpricot.XML(open(url))

You can also add this to your .irbrc:

def toggle_output
IRB.conf[:MAIN_CONTEXT].echo = ! IRB.conf[:MAIN_CONTEXT].echo
end
 
B

Brian Candler

Don said:
require 'hpricot'
url = "http://www....."
doc = Hpricot.XML(open(url))

In interactive ruby can I suppress the output produced by
Hpricot.XML(open(url))

is this the stdout stream or is it something else?

In Ruby, every expression has a value, and by default irb displays that
value (more precisely, it shows value.inspect)

For example:
3
=> nil
In the second example, "3" is puts producing output, and the return
value from puts is nil.
 
D

Don Norcott

Thanks these all did the trick. I especially like the toggle.

However even after finding the solution I still could not find where any
of this is documented.

I would like to see an explanation of "string"; null.

I find that adding only a semicolon (no nil)does the trick
doc = Nokogiri::HTML(open(url)); # produces no display


Can you point me to some documentation than may explain the solutions,
so I know where to look next time.

Thanks
 
A

Adam Prescott

It won't be documented anywhere in the way you're expecting. It's
actually already been explained in these responses.

IRB will print, as the return value, the "last" value of the line you
give it (using inspect), so

1; 2

will return "=> 2" because that's the "last" value of that line.
Adding "; nil" just stops everything before "; nil" from being printed
as output.
 
R

Ryan Davis

Thanks these all did the trick. I especially like the toggle.
=20
However even after finding the solution I still could not find where = any=20
of this is documented.

Sure it is. It is documented everywhere. =46rom the pickaxe:

"irb will display the value of each expression as you complete it. For =
instance: [...irb examples...]"
I would like to see an explanation of "string"; null.

nil, not null.

What more explanation do you want to see, and where? If you have a lot =
of output from an expression, you augment that expression so that it =
isn't the final result.=20
I find that adding only a semicolon (no nil)does the trick
doc =3D Nokogiri::HTML(open(url)); # produces no display

No, it doesn't. Look at my prompts:
501 % irb
?>=20

You've told ruby (irb actually) that there is more coming. That's all.
Can you point me to some documentation than may explain the solutions,=20=
so I know where to look next time.

Again. Everywhere. The pickaxe has an entire chapter on the subject.=
 
D

Don Norcott

Sorry I seem to hit a sore spot. I have no problem with the
answers/explanations given. I have read multiple tutorials but they are
not a good reference source. I have downloaded both core and std
library documentation but it is difficult to retrieve info from as you
must know what your after before you start looking.

I guess I am looking for a non-HTML document that contains the Ruby
spec(like the 'ANSI C Standard' documentation). If such a creature
exists I would appreciated a pointer to it.

I probably did not phrase my original query properly. It had at least
as much to do with finding the documentation that would answer my
questions as opposed to simply looking for an answer.
 
M

Mark T

Ruby 1.8.7 is not .quite. an ISO standard yet.
It's close to being a Japanese Industrial Standard.

http://www.infoq.com/news/2010/03/ruby-standardization
http://ruby-std.netlab.jp/

There is a pdf in amongst these links.

All good!

MarkT
I guess I am looking for a non-HTML document that contains the Ruby
spec(like the 'ANSI C Standard' documentation). If such a creature
exists I would appreciated a pointer to it.

--=20
(+61 4) 0679 5734 :: skype: govirtual.com.au :: =E3=83=81=E3=82=A7=E3=83=83=
=E3=82=AF=E3=82=A2=E3=82=A6=E3=83=88=E3=81=8C=E3=80=81Jingle =E3=81=A0!
::It's a Jingle Out There!
 
R

Ryan Davis

=20
Sorry I seem to hit a sore spot. I have no problem with the=20
answers/explanations given. I have read multiple tutorials but they = are=20
not a good reference source. I have downloaded both core and std=20
library documentation but it is difficult to retrieve info from as you=20=
must know what your after before you start looking.
=20
I guess I am looking for a non-HTML document that contains the Ruby=20
spec(like the 'ANSI C Standard' documentation). If such a creature=20
exists I would appreciated a pointer to it.

There is [1], but like the ANSI C standard, it only goes over the core =
language. I don't think there is an effort to define a specification for =
the ruby standard library.


[1]: =
http://www.ipa.go.jp/software/open/ossc/english/ruby/ruby_draft_specificat=
ion.html=
 
B

Brian Candler

Don said:
I have read multiple tutorials but they are
not a good reference source. I have downloaded both core and std
library documentation but it is difficult to retrieve info from as you
must know what your after before you start looking.

I learned most from this:
http://ruby-doc.org/docs/ProgrammingRuby/
and I still highly recommend it.

That's the free 1st edition, written for ruby 1.6, but pretty much all
applies to 1.8. I think the book source is floating around somewhere,
and somebody may have made a PDF of it.

Otherwise, it's certainly worth buying the PDF of the 2nd edition. I
would not buy the 3rd edition unless you want to deal with the issues
around ruby 1.9.
 
D

Don Norcott

Thanks all, this is exactly what I was looking for.

I had previously read a sample chapter of pragmatic programmer and was
impressed by its clarity. So will go through this book in its entirety.

Thanks again.
Don
 

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

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top