Documentation for Psych to_yaml options? - ruby

Ruby 1.9.3 defaults to using Psych for YAML. While the ruby-doc documentation for it is completely lacking, I was able to find one external piece of documentation that hinted that the indentation option is supported. This was borne out in testing:
irb(main):001:0> RUBY_VERSION
#=> "1.9.3"
irb(main):002:0> require 'yaml'
#=> true
irb(main):003:0> [[[1]]].to_yaml
#=> "---\n- - - 1\n"
irb(main):009:0> [[[1]]].to_yaml indentation:9
#=> "---\n- - - 1\n"
There are presumably more options supported. Specifically, I want to know how to change the line wrap width or disable it altogether.
What are the options available?

Deep in the guts of ruby-1.9.3-p125/ext/psych/emitter.c I found three options:
indentation - The level must be less than 10 and greater than 1.
line_width - Set the preferred line width.
canonical - Set the output style to canonical, or not (true/false).
And they work!

When you want to disable line wrap, use this option:
line_width: -1

Related

Ruby frozen string comment syntax difference

There are two ways of enabling all strings in a file to be implicitly frozen1.
# frozen-string-literal: true
# frozen_string_literal: true
Is there a difference between these two syntaxes?
Thanks!
The answer you link to never uses the magic comment # frozen-string-literal: true only # frozen_string_literal: true. The difference is that only the latter will work.
The other way to enable frozen string literals is to run the application with the --enable=frozen-string-literal flag.

How can I comment out my Ruby return values with something like "# =>"?

Having just started with Ruby and while following a tutorial, the result was shown as:
a + b # => 3
I have never seen such a possibility; that seems so handy! Could you please tell me what it is? is it proprietary or is it for everyone?
Josh Cheek's seeing is believing. Apparently you can run it over your code, or it can be integrated in several editors.
Reconfigure Your REPL
The # symbol is a comment in Ruby. By default, most Ruby REPLs (e.g. irb or pry) will use => to prefix the return value of your last expression.
In IRB, you can modify this prefix so that each return value is prefixed by a different string. You can do this through the IRB::Context#return_format method on your conf instance. For example:
$ irb
irb(main):001:0> conf.return_format = "#=> %s\n"
#=> "#=> %s\n"
irb(main):002:0> 1 + 2
#=> 3
More permanent changes would have to be made in your IRB configuration file by customizing the prompt through the IRB.conf[:PROMPT] Hash and then setting IRB.conf[:PROMPT_MODE] to your custom prompt, but in my opinion the solution above is simpler even though you have to run it within the current REPL session rather than saving it as a default.

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

Confusion with the output of %r/..../option.to_s

I was playing to see how Regex#to_s disable options with the pattern %r/../. But getting confused with the output of such Regex#to_s :
irb(main):005:0> %r/ab+c/x.to_s
=> "(?x-mi:ab+c)" #why here -m option has been disabled?
irb(main):006:0> %r/ab+c/i.to_s
=> "(?i-mx:ab+c)" #why here -m option has been disabled?
irb(main):007:0> %r/ab+c/m.to_s
=> "(?m-ix:ab+c)" #why here -i option has been disabled?
irb(main):008:0> %r/ab+c/o.to_s
=> "(?-mix:ab+c)" #why here o option not get into the (...) as the above?
irb(main):009:0> %r/ab+c/.to_s
=> "(?-mix:ab+c)" #why always m,i,x option get into the (...) as the above?
Could anyone help me here to understand the logic on which based the option are getting on/off?
How do the Regex#hash and Regex#quote methods work in Ruby 1.9.3 (any small code to understand the same)?
I think your understanding is inverted; the options on the left of the dash are on, while the options to the right of the dash are off.
/ab+c/x => "x-mi"
/ab+c/i => "i-mx"
/ab+c/m => "m-ix"
Each of the three options appears in each regex string, but their presence to the left or right of the dash indicates whether the option is on or off.
Regarding your second question, Regexp#hash is simply a method that generates the same Fixnum for a given Regexp. This allows you to compare two different Regexp options for effective equality. See Object#hash for more details.

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

Resources