Check ruby version to puts message [closed] - ruby

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.

Related

Bash - Increment the version number by 1 [closed]

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 1 year ago.
Improve this question
I'm new to bash script. I need to bump up the version number of my build by 1.
For eg:- if my version is 1.0.0+1, bumping it would generate 1.0.1+2, ie basically incrementing the number by 1 before and after +.
How can this be done in bash script?
The bash approach which is basically close to Python style is to use a regular expression to separate the version number and the rest of the version string:
version=1.0.0+1
if [[ $version =~ ^([^+?]*[.])([0-9]*)[+]([0-9]+) ]]
then
# ... see below
else
echo Unexpected version string: "$version"
fi
When you have a match, you can refer to the capture groups in the regexp. In Python you would use something like result.group(1) to access the groups. In bash, you have for this purpose a single, predefined array BASH_REMATCH, which always refers to the most recent regexp matching. In our case, we would have
version_string_head=${BASH_REMATCH[1]} # 1.0.
version_count_hi=${BASH_REMATCH[2]} # 0
version_count_lo=${BASH_REMATCH[3]} # 1
The rest is trivial:
new_version_string=${version_string_head}$((version_count_hi + 1))+$((version_count_lo + 1))

[[:punct:]] matches differently in irb and rails test [duplicate]

This question already has an answer here:
Regex "punct" character class matches different characters depending on Ruby version
(1 answer)
Closed 5 years ago.
[[:punct:]] doesn't match any punctuation when it's called by a rails model test. Using the following code
test "punctuation matched by [[:punct:]]" do
punct_match = /\A[[:punct:]]+\Z/.match('[\]\[!"#$%&\'()*+,./:;<=>?#\^_`{|}~-]')
puts punct_match
puts punct_match.class
end
this outputs a non-printable character and NilClass.
However, if I execute the same statement
punct_match = /\A[[:punct:]]+\Z/.match('[\]\[!"#$%&\'()*+,./:;<=>?#\^_`{|}~-]')
in irb matches correctly and outputs
[\]\[!"#$%&'()*+,./:;<=>?#\^_`{|}~-]
=> nil
What am I missing?
Ruby version => 2.2.4,
Rails version => 4.2.6
The behaviour of /[[:punct:]]/ changed slightly in ruby version 2.4.0.
This bug was raised in the ruby issues, which links back to this (much older) issue in Onigmo - the regexp engine used since Ruby 2.0+.
The short answer is, these characters were not matched by /[[:punct:]]/ in ruby versions <2.4.0, and are now matched:
$+<=>^`|~
You must be running irb in a newer ruby version than this rails application, which is why it matches there.
On a separate note, you should alter your code slightly to:
/\A[[:punct:]]+\z/.match('[]!"#$%&\'()*+,./:;<=>?#^_`{|}~-]')
Use \z, not \Z. There is a slight difference: \Z will also match a new line at the end of the string.
You have unnecessary back-slashes in the string, such as '\^'
You have repeated a [ character: '[\]\['

find and replace percent symbol within string with back slash in ruby? [closed]

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

How to use the below description mentioned symbols? [closed]

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.

Ruby cannot find puts and other commands? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I was attempting to do what I thought should be a very basic script as my first bit of ruby code. Perhaps someone can help me out with what's going on.
./delete_file.rb: line 3: puts: command not found
./delete_file.rb: line 4: auth_token: command not found
./delete_file.rb: line 6: puts: command not found
User1$ cat delete_file.rb
# /usr/bin/ruby
# Delete file script
puts "Enter current token"
auth_token = gets.chomp
puts "data goes here" + auth_token + "more data here"
first line of file should be:
#!/usr/bin/ruby

Resources