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.
Related
I'm struggling with the xml-simple (1.1.5) gem.
This is my input in test.xml:
<bib>
<title><br/>X</title>
<title>X<br/>X</title>
<title>X<br/></title>
</bib>
Now see what happens using irb as follows:
$ irb -rxmlsimple -rpp
>> pp XmlSimple.xml_in("test.xml")
{"title"=>
[{"br"=>[{}], "content"=>"X"},
{"br"=>[{}], "content"=>["X", "X"]},
{"br"=>[{}], "content"=>"X"}]}
=> {"title"=>[{"br"=>[{}], "content"=>"X"}, {"br"=>[{}], "content"=>["X", "X"]}, {"br"=
>>
So apparently the first and last records, though different, give the same hashes in the output.
Is this a bug?
The xml-simple gem does not work reliably with mixed content. Here's an extract from its documentation:
Mixed content (elements which contain both text content and nested elements) will be not be represented in a useful way - element order and significant whitespace will be lost. If you need to work with mixed content, then XmlSimple is not the right tool for your job.
I was just playing around with interactive ruby.
Near the beginning (line 138), I did
. irb(main):138:0> ['rock','paper','scissors'].index('paper')
=> 1
And that above worked
Then I tried a bunch of lines 139-147 experimenting to get more used to the language
Then I wasn't getting results and I tried some even simpler things I expected would work, 148-154 and didn't get any result.
So it looks like at some point one of my commands might've stopped it from displaying results though i'm not sure what.
I'd like to get it to display the results again. I suppose I could try to exit and go back in but i'd rather a way without doing that,
. irb(main):138:0> ['rock','paper','scissors'].index('paper')
=> 1
irb(main):139:0> a=[1,2,3
irb(main):140:1> a
irb(main):141:1> a=[1.2.3]
irb(main):142:1> a[0]
irb(main):143:1> a(0)
irb(main):144:1> a=[1,2,3]
irb(main):145:1> a(1)
irb(main):146:1> puts a(1)
irb(main):147:1> puts a[1]
irb(main):148:1> a
irb(main):149:1> a=[1,2,3]
irb(main):150:1> a
irb(main):151:1> h={4=>4}
irb(main):152:1> h
irb(main):153:1> puts 6
irb(main):154:1>
If it makes any difference this is my version number and OS is Windows.
C:\blah>ruby -v
ruby 2.1.6p336 (2015-04-13 revision 50298) [i386-mingw32]
C:\blah>
Because of this line here:
irb(main):139:0> a=[1,2,3
You haven't closed off the array with a closing ]. the :1 in irb(main):154:1> makes it clear you're inside a nested expression.
If you enter another ], you'll get a big syntax error because all of what you've entered before it isn't valid array syntax, but then you can move on.
Notice that since late 2019, assignment does not output any more the value in irb.
This is a new case where Interactive Ruby (irb) stop showing result.
NB : you can get the old behavior by setting IRB.conf[:ECHO_ON_ASSIGNMENT] = true in your ~/.irbrc.
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
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.
Are there any tools that'd indicate that your code can produce different results in different ruby versions?
I'm envisaging something like
a = 1
b = 2
string = [1, 2, 3, 4].to_s
ToolName: Array#to_s has different behaviour in Ruby 1.8 and 1.9
Or failing that, inspect each variable at each line and indicate the first point at which they diverge under different versions, such as:
Ruby1.8:
a: 1 at line 1
b: 2 at line 2
string: "1234" at line 3
Ruby1.9:
a: 1 at line 1
b: 2 at line 2
string: "[1, 2, 3, 4]" at line 3
Ruby1.8 and Ruby1.9 first differ at line 3
Or is the only approach currently available to unit test your code, and make sure the tests pass on all versions of ruby?
The gem One9 allows you to run a test suite, and it'll report which methods you've called that have changed between 1.8 and 1.9.
I haven't heard of anything, though a combination of rvm and good tests could help you tell.
rvm has the ability to cycle through all the rubies you have installed and execute a given script in each one. If that script was a suite of tests you might ferret out any version-based misdeeds. Set Actions is where you'll want to read for that rvm goodness.