How to get the Ruby documentation from the command line [duplicate] - ruby

This question already has an answer here:
Why does my Ruby 'ri' tool not return results in command prompt? [duplicate]
(1 answer)
Closed 9 years ago.
The community reviewed whether to reopen this question 3 months ago and left it closed:
Original close reason(s) were not resolved
Is there a way to find out which part of my ri command that is not showing Ruby's documentation:
$ ruby --version
ruby 1.9.3p392 (2013-02-22 revision 39386) [i686-linux]
$ ri --version
ri 3.12.2
$ ri String
Nothing known about String
When I use pry:
$ pry --version
Pry version 0.9.12 on Ruby 1.9.3
$ pry
[1] pry(main)> ri String
# shows String documentation
[2] pry(main)> ri String.split
error: 'String.split' not found
[3] pry(main)> ri String.strip
String.strip not found, maybe you meant:
String#strip_heredoc
What should I do to make the documentation appear?

If you're using RVM to manage your Ruby installations you can do this:
rvm docs generate
If not, try doing this:
gem install rdoc-data
rdoc-data --install
then try the ri command again.

With pry, it's better to install the pry-doc gem, and then use the show-doc command:
[17] pry(main)> show-doc String#inspect
From: string.c (C Method):
Owner: String
Visibility: public
Signature: inspect()
Number of lines: 6
Returns a printable version of _str_, surrounded by quote marks,
with special characters escaped.
str = "hello"
str[3] = "\b"
str.inspect #=> "\"hel\\bo\""
[18] pry(main)> show-doc Array#pop
From: array.c (C Method):
Owner: Array
Visibility: public
Signature: pop(*arg1)
Number of lines: 11
Removes the last element from self and returns it, or
nil if the array is empty.
If a number n is given, returns an array of the last n elements
(or less) just like array.slice!(-n, n) does. See also
Array#push for the opposite effect.
a = [ "a", "b", "c", "d" ]
a.pop #=> "d"
a.pop(2) #=> ["b", "c"]
a #=> ["a"]
[19] pry(main)>
Note: you can also use the ? alias for show-doc if you prefer.

You mentioned in a comment that you're using the Ruby package from archlinux's package manager. What you need for ri is to install the ruby-docs package:
$ pacman -S ruby-docs
I guess they separate the packages so people who don't want the docs can save on disk usage.

When I use pry:
$ pry --version
Pry version 0.9.12 on Ruby 1.9.3
$ pry
[1] pry(main)> ri String
# shows String documentation
[2] pry(main)> ri String.split
error: 'String.split' not found
[3] pry(main)> ri String.strip
String.strip not found, maybe you meant:
String#strip_heredoc
What should I do to make the documentation appear?
Well, there are no methods String.split or String.strip. There are, however, methods String#split and String#strip. Try asking for those, and you'll probably get their documentation.

Related

Format byebug's output

I've recently done a fresh install of my system and I'm seeing some odd behaviour from byebug. For instance, arrays are listed on element on line instead of having everything on the same line. Here is an example:
(byebug) [1,2,3]
1
2
3
Normally, I would expect:
(byebug) [1,2,3]
[1, 2, 3]
How can I get byebug to display arrays "normally"?
The issue got fixed with the latest versions of byebug: gem 'byebug', '~> 8.0'. Arrays are properly printed now.

Pulling Ruby version from regular expression

I am trying to return the version of Ruby (such as 2.1.0) from a regular expression. Here's the string as it should be evaluated by a regular expression:
ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-darwin12.0]\n
How would I go about extracting 2.1.0 from this? It seems to me that the best way to do this would be to extract the numbers around two periods, but no spaces or characters around them. So basically, it would pull just 2.1.0 instead of anything else.
Any ideas?
How about:
str = "ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-darwin12.0]\n"
str[/[\d.]+/] # => "2.1.0"
[\d.]+ means "find a string of characters that are digits or '.'.
str[/[\d.]+/] will find the first such string that matches and return it. See String#[] for more information.
The question is, do all versions and Ruby interpreters return their version information consistently? If your code could end up running on something besides the stock Ruby you might have a problem if the -v output changes in a way that puts the version farther into the string and something else matches first.
TinMan, I think you need a more rigorous regex; e.g., "1..0"[/[\d.]+/] => "1..0", "2.0.0.1."[/[\d.]+/] => "2.0.0.1.", "2.0.0.0.1"[/[\d.]+/] => "2.0.0.0.1"
Ruby uses a similar style to Semantic Versioning, so the actual format of the string shouldn't vary, allowing a simple pattern. Where the version number occurs might not be defined though.
IF it went crazy, something like /[\d.]{3,5}/ should herd things back into some semblance of order, and normalize the returned value:
[
'foo 1.0 bar',
'foo 1.1.1 bar',
'foo 1.1.1.1 bar'
].map{ |s| s[/[\d.]{3,5}/] }
# => ["1.0", "1.1.1", "1.1.1"]
If you're trying to do this with running code, why not use the predefined constant RUBY_VERSION:
RUBY_VERSION # => "2.1.2"
Version numbers are notoriously difficult to grab, because there are so many different ways that people use to format them. Over the last several years we've seen some attempts to create some order and commonality.
Edit: I misread the question. I assumed the given string might be embedded in other text, but on re-reading I see that evidently is not the case. The regex given by #theTinMan is sufficient and preferred.tidE
This is one way:
str = "ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-darwin12.0]\n"
str[/[Rr]uby\s+(\d\.\d\.\d)/,1]
#=> "2.1.0"
This could instead be written:
str[/[Rr]uby\s+(\d(\.\d){2})/,1]
If matching "Ruby 2.1" or Ruby "2" were desired, one could use
str[/[Rr]uby\s+(\d(\.\d){,2})/,1] # match "2", "2.1" or "2.1.1"
or
str[/[Rr]uby\s+(\d(\.\d){1,2})/,1] # "2.1" or "2.1.1", but not "2"
Just Inspect RUBY_VERSION
Rather than parsing the output of whatever you're trying to parse, just inspect the RUBY_VERSION constant. On any recent Ruby, you should get output similar to the following in a REPL like irb or pry:
RUBY_VERSION
# => "2.1.0"
Or ask Ruby on the command line with:
$ ruby -e 'puts RUBY_VERSION'
2.1.0
Try this:
str = "ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-darwin12.0]\n"
pieces = str.split(" ", 3)
version, patch_num = pieces[1].split('p')
puts version
--output:--
2.1.0

Ruby 1.8.7 RegExp doesn't work with meta-character "\h"

If I run this in a 1.8.7 console:
$ irb-ruby-1.8.7-p330
1.8.7 :001 > "0a" =~ /\h\h/
=> nil
And if I run the same in a 1.9.2 console:
$ irb-ruby-1.9.2-p290
1.9.2p290 :001 > "0a" =~ /\h\h/
=> 0
:/
You're right that \h doesn't seem to be recognised by the standard Ruby 1.8.7 regexp library. This can be confirmed using Rubular. If you need 1.8 compatibility in your code without using any additional gems I think your only alternative is to use an equivalent character class [0-9a-fA-F].

ruby incorrect method behavior (possible depending charset)

I got weird behavior from ruby (in irb):
irb(main):002:0> pp "    LS 600"
"\302\240\302\240\302\240\302\240LS 600"
irb(main):003:0> pp "    LS 600".strip
"\302\240\302\240\302\240\302\240LS 600"
That means (for those, who don't understand) that strip method does not affect this string at all, same with gsub('/\s+/', '')
How can I strip that string (I got it while parsing Internet page)?
The string "\302\240" is a UTF-8 encoded string (C2 A0) for Unicode code point A0, which represents a non breaking space character. There are many other Unicode space characters. Unfortunately the String#strip method removes none of these.
If you use Ruby 1.9.2, then you can solve this in the following way:
# Ruby 1.9.2 only.
# Remove any whitespace-like characters from beginning/end.
"\302\240\302\240LS 600".gsub(/^\p{Space}+|\p{Space}+$/, "")
In Ruby 1.8.7 support for Unicode is not as good. You might be successful if you can depend on Rails's ActiveSupport::Multibyte. This has the advantage of getting a working strip method for free. Install ActiveSupport with gem install activesupport and then try this:
# Ruby 1.8.7/1.9.2.
$KCODE = "u"
require "rubygems"
require "active_support/core_ext/string/multibyte"
# Remove any whitespace-like characters from beginning/end.
"\302\240\302\240LS 600".mb_chars.strip.to_s

Ruby: hexadecimal in regular expressions

I need to match an md5 checksum in a regular expression in a Ruby (actually Rails) program. I found out somewhere that I can match hexadecimal strings with \h sequence, but I can't find the link anymore.
I'm using that sequence and my code is working in Ruby 1.9.2. I can make it working even under plain IRB (so it's not a Rails extension).
ruby-1.9.2-p180 :007 > "123abcdf" =~ /^\h+$/; $~
=> #<MatchData "123abcdf">
ruby-1.9.2-p180 :008 > "123abcdfg" =~ /^\h+$/; $~
=> nil
However my IDE mark that expression as wrong and I can't find any reference which cites that sequence.
Is the \h sequence legal in Ruby Regex under any environment/version or should I trust my ide and replace it with something like [abcdef\d]?
Yes it is. Check the official doc for the complete documentation for regex in Ruby.
Note that \h will match uppercase letters too, so it's actually equivalent to [a-fA-F\d]
According to this \h is part of oniguruma, which I believe is standard in ruby 1.9.

Resources