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

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

Related

raku: Support for utf8-c8 issue in Raku 2020.10 [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 2 years ago.
Improve this question
I have a file from Wild Wild Web and it contains malformed UTF8. I handled malformed UTF8 in my other codes in previous versions of Raku. In 2020.10 version, I am running into this issue below. Has the support for utf8-c8 changed (this page says it should work, but it doesn't seem to) :
https://docs.raku.org/language/unicode#index-entry-UTF-8_Clean-8
This page has this example:
say slurp($test-file, enc => 'utf8-c8');
Now my code on the command line:
raku -e 'my $a = slurp("zlist"); for $a.lines { .say }'
Malformed UTF-8 near bytes 73 e2 5f at line 55 col 14
in block <unit> at -e line 1
Then using this:
raku -e 'my $a = slurp("zlist", enc => 'utf8-c8'); for $a.lines { .say }'
===SORRY!=== Error while compiling -e
Undeclared routine:
utf8-c8 used at line 1
My code is simple and essentially copied from the example. What am I doing wrong?

Bash string literal comparison to variable failing [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 4 years ago.
Improve this question
I have the following simple bash code to test string comparison:
#!/bin/sh
BRANCH="master"
echo $ref
if [[ "$ref" = "refs/heads/$BRANCH" ]]
then
echo "Matches"
else
echo "Do not match"
fi
When I ran the code using export ref=/refs/heads/master && . sample I get the following result:
/refs/heads/master
Do not match
What may be causing the problem?
What is causing the problem is the missing slash in your test: /refs/heads/master is not equal to refs/heads/master!

ruby Unexpected keyword else error [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 6 years ago.
Improve this question
I have this ruby program to parse a CSV file. I am missing a endif somewhere and I cannot figure out.
require 'csv'
prevrow=nil
newarray=Array.new
CSV.foreach("\\\\192.168.0.1\\fe18cb0618cabd41\\ninjatrader\\uniqueside.csv", col_sep: ',') do |row|
if(prevrow==nil)
# do nothing
newarray<<row
prevrow=row
elsif (prevrow!=nil and row[0]!=prevrow[0] )
# do something
newarray<<row
prevrow=row
##count=1
elsif(prevrow!=nil and row[0]=prevrow[0] and ##count<4)
puts "new date"
newarray<<row
prevrow=row
##count++
end
end
removesamedirctiontop4.rb:23: syntax error, unexpected keyword_else
removesamedirctiontop4.rb:27: syntax error, unexpected end-of-input, expecting keyword_end
#count++ is not valid ruby. The final "plus" is expecting another parameter and thinks it's on the next line, so the line ends up being interpreted as...
`#count + +end`
So you have an invalid statement and you lose an end.
Change the offending line to
#count += 1

Append to variable within shell script loop [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'm trying to loop over all environment variables in a shell script, and create an HTML query string from ones which match a pattern. Unfortunately, I can't seem to assign to variables in the loop. I've got this:
#!/bin/sh
IFS=$'\n'
TAGS=""
for item in $(printenv)
do
if [[ $item == FOO_TAG_* ]]
then
TAGS = "${TAGS}&${item}"
fi
done
But this gives me
/etc/script.sh: line 9: TAGS: command not found
/etc/script.sh: line 9: TAGS: command not found
How do I fix this?
In the assignment, remove space between variable name and =
TAGS="${TAGS}${item}"

Check ruby version to puts message [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
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.

Resources