How to suppress display of specific code in irb?

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

On Oct 3, 2010, at 19:33 , Don N. wrote:

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

easiest way is:

Hpricot.XML(open(url)); nil

On 10/03/2010 07:33 PM, Don N. wrote:

Hpricot.XML(open(url))

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

On Oct 3, 2010, at 19:33 , Don N. wrote:

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

Don N. wrote:

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:

1+2
=> 3

puts 1+2
3
=> nil

In the second example, “3” is puts producing output, and the return
value from puts is nil.

On Mon, Oct 4, 2010 at 12:20 PM, Brian C. [email protected]
wrote:

Don N. wrote:

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?

You can set the ECHO configuration off:

IRB.conf[:ECHO] = false

HTH,
Ammar

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.

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

On Oct 4, 2010, at 14:55 , Don N. wrote:

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.

Sure it is. It is documented everywhere. From 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.

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

No, it doesn’t. Look at my prompts:

501 % irb

“blah”
=> “blah”

“blah”;
?>

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,
so I know where to look next time.

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

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.

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

On Oct 4, 2010, at 18:53 , Don N. wrote:

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.

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.

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

Don N. wrote:

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.

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