Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Where are these symbols defined, and what are they used for?
:w2_end
:w2_beg
:w1_beg
:w1_end
I found those in my IRB by using the line Symbol.all_symbols .
My Ruby version and IRB versions are:
C:\>ruby -v
ruby 1.9.3p374 (2013-01-15) [i386-mingw32]
C:\>irb --version
irb 0.9.6(09/06/30)
I tried the same in another Ruby and IRB version as below:
C:\>irb --version
irb 0.9.6(09/06/30)
C:\>ruby -v
ruby 1.9.3p392 (2013-02-22) [i386-mingw32]
Arr = Symbol.all_symbols
Arr.include?(:w2_end) #=> true
Arr.include?(:w2_beg) #=> true
Arr.include?(:w1_beg) #=> true
Arr.include?(:w1_end) #=> true
These symbols don't appear in the Ruby source, nor are they defined when I look for them:
$ rvm 1.9.3-p374 do irb
1.9.3p374 :003 > Symbol.all_symbols.map(&:to_s).grep(/^w\d/)
=> []
Have you got your irb configured to load any extensions? Look in your .irbrc, if you have one.
Those symbols are commonly found in, among other places, readline libraries. irb uses readline. Perhaps there's something special about readline on Windows (it being coded in Ruby, for example) that causes those symbols to be defined.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I tried this :
irb(main):125:0> a = "ab%c"
=> "ab%c"
irb(main):126:0> a.gsub("%", '\\')
=> "ab\\c"
irb(main):127:0>
whereas expected output is:
ab\c
it did not work.
Thanks in advance.
Update: ruby version
ruby -v
ruby 1.8.7 (2011-06-30 patchlevel 352) [x86_64-linux]
a = "ab%c"
a.gsub!("%", '\\')
#=> "ab\\c"
puts a
# ab\c
in "ab\\c" backslash \ is being escaped using character \.
you can verify this with puts
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
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am trying to test if the user has a Ruby version greater than or equal to 1.9.0, and if not, update it.
output = `ruby -v`
if !output ~> "1.9.0"
But as I read, if the version is 1.8.7, then it would say that it is above 1.9.0 since ~> means that the version is approximately greater than the last digit.
Correction to the original question , the ~> is not valid, was a misinterpretation to a gem specific thing I read.
You can use strings
RUBY_VERSION < '1.9.0'
There are a few gems available for this.
Versionomy is probably the most popular. You'll use something like this:
require 'versionomy'
v1 = Versionomy.parse('0.1')
v2 = Versionomy.parse('0.2.1')
v3 = Versionomy.parse('0.44')
v1 < v2 # => true
v2 < v3 # => true
v1 > v2 # => false
v2 > v3 # => false
Also note that there's a top level constant RUBY_VERSION you can use the get the Ruby version within rails.
Edit: If you simply want to check the second digit you can use:
version = RUBY_VERSION.split(".")[1]
This will return '9' for 1.9.2.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a program test.rb I want to be able to pass arguments to the program, much like you can do in the C language. For example:
ruby test.rb param1
Is there a way I can do this?
Use ARGV
The special ARGV array contains the arguments passed to a Ruby script. For example:
$ ruby -e 'puts ARGV.inspect' param1
["param1"]
See also ARGF#argv.
Ruby has a lib included that handles command line option: Options Parser
http://ruby-doc.org/stdlib-2.0.0/libdoc/optparse/rdoc/OptionParser.html
A simple example ruby program:
require 'optparse'
# create hash to hold program options
program_options = {}
# Parse the options passed into via command line
OptionParser.new do |opts|
opts.banner = "Usage: example.rb [options]"
opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
options[:verbose] = v
end
end.parse!
# Run Program Code
# ...
# your program will have access to the program_options hash
then run the program with ruby example.rb -v
opts.on has many options for detecting long (i.e., --verbose) and short (i.e., -v) switches, as well as accepting input and optional switches
How do I have my ruby script output what version of ruby is running it?
The RUBY_VERSION constant contains the version number of the ruby interpreter and RUBY_PATCHLEVEL contains the patchlevel, so this:
puts RUBY_VERSION
outputs e.g. 2.2.3, while this:
puts RUBY_PATCHLEVEL
outputs e.g. 173. Together it can be used like this:
ruby -e 'print "ruby #{ RUBY_VERSION }p#{ RUBY_PATCHLEVEL }"'
to output e.g. ruby 2.2.3p173
For reference, here's how variables and constants work, along with a list of Ruby's built-in variables and constants: Ruby Programming/Syntax/Variables and Constants and Pre-defined Variables.
You can get a list of all the global constants here, including RUBY_VERSION and friends, in the official Ruby language documentation.
For the bonus round, this will tell you some more useful info about your Ruby environment using RbConfig:
require 'rbconfig'
puts Config::CONFIG.sort_by{ |n,v| n.downcase }.map{ |n,v| "#{n} => '#{v}'" }