I am running test with Capybara on Ruby. I got an error for this code
pos = rand languages.size
# advanced_search.adv_language_labels[pos].click
script = "$('label[for*='lang']:eq(" + pos.to_s + ")').click()"
page.execute_script script
How can I get this 'pos' value to the script so that I can run with execute_script command? Thanks.
The string you're generating looks like this:
$('label[for*='lang']:eq(5)').click()
Note there's an internal pair of single quotes that's closing out the label path before you mean to. You need to escape those.
$('label[for*=\'lang\']:eq(5)').click()
Related
Am trying to bring output like below in AWS console for Cloudwatch metric filter pattern
[w1,w2,w3,w4=!"*10.1.1.1*"&&w5=!"*10.1.1.2*"&&w5="*admin*"]
for one of my ruby aws sdk script with below function
data_of_ips = ["10.1.1.1", "10.1.1.2"]
def run_me(data_of_ips)
:::
:::
filter_pattern = '[w1,w2,w3,w4!='"*#{data_of_ips[0]*"'&&w4!='"*#{data_of_ips[1]*"',w5="*admin*"]'
but in the aws console output I could see the output like below without " (double quotes) for w4.
[w1,w2,w3,w4=!*10.1.1.1*&&w4=!*10.1.1.2*&&w5="*admin*"]
Please help me out to fix this.
Is this what you expect?
data_of_ips = ["10.1.1.1", "10.1.1.2"]
def run_me(data_of_ips)
"[w1,w2,w3,w4!=\"*#{data_of_ips[0]}*\"&&w4!=\"*#{data_of_ips[1]}*\"&&w5=\"*admin*\"]"
end
puts run_me(data_of_ips)
# => [w1,w2,w3,w4!='"*10.1.1.1*"&&w4!="*10.1.1.2*"&&w5="*admin*"]
If you want to put variable or script inside a string, I suggest you can use double quote with #{} to do that. It's more clear. And the special char like " you can just use \" to write them.
I fixed it this way:
filter_pattern = '[w1,w2,w3,w4!="'"*#{data_of_ips[0]}*"'"&&w4!="'"*#{data_of_ips[1]}*"'"w5="*admin"]'
I used "'" (double-quotes single-quote double-quotes) and got the expected output below:
filter_pattern = '[w1,w2,w3,w4!="*10.1.1.1*"&&w4!="*10.1.1.2*",w5"*admin*"]'
how can i split a string "DESKTOP-AHDESI\Username" by slash in ruby 2.7.1p83
tmp = "DESKTOP-AHDESI\Username"
print tmp
tmp = tmp.split("\\")
print tmp
i got:
Ruby Error: NoMethodError undefined method `gsub!'
Problem
Your tmp variable is enclosed in double-quotes, and contains a backslash which is being interpreted as an escape rather than a character literal. You can see this easily by simply pasting your string into a REPL like irb:
"DESKTOP-AHDESI\Username" #=> "DESKTOP-AHDESIUsername"
You need to handle the backslash specially in both your String methods.
Solution
One way to handle this is to use Ruby's alternate quoting mechanism. For example:
%q(DESKTOP-AHDESI\Username).split '\\' #=> ["DESKTOP-AHDESI", "Username"]
This may not help you directly, though.
Wherever the value from tmp is coming from, you need to refactor the code to ensure that your String is properly escaped before you assign it to your variable, or otherwise pre-process it. String#dump won't really help much if the value you're assigning is unescaped before assignment, so you're going to have to fix this in whatever code you're using to generate or grab the string in the first place.
First of all, you are giving the wrong string. \ is the escape character when you use inside the "". So It will try to escape the next character U but this character doesn't have any Job so it will print U on the screen. Modify your string like below, it will work.
tmp = "DESKTOP-AHDESI\\Username"
p tmp
tmp = tmp.split("\\")
p tmp
Output
"DESKTOP-AHDESI\\Username"
["DESKTOP-AHDESI", "Username"]
When I try to shorten an array with '' the output from all other variables is changing too
message = "bot.start"
seperator = message
command = seperator
command[0..3] = ''
message #=> "start"
The output should be "bot.start". Ruby should have a problem separating variables from each other. What is wrong?
In the current version Ruby, strings are mutable. That is, you can change an instance of a string.
In your example, message, command and separator are all different variables that point to the same string instance. When you do [0..3] = '', you are changing the string that all the variables point to.
If you need to make distinct instances, use dup to copy the string:
command = seperator.dup
Alternatively, don't modify the string and use APIs that return a new instance of a string:
command = seperator[4..-1]
When you execute line 4
command[0..3] = ''
You grabed bot. and changed to bot. => ''
That's why it returns''start which is start
https://repl.it/repls/BlushingThoughtfulOrigin
I am pulling the following string from a CSV file, from cell A1, and storing it as a variable:
#{collector_id}
So, cell A1 reads #{collector_id}, and my code essentially does this:
test = #excel_cell_A1
However, if I then do this:
puts test
I get this:
#{collector_id}
I need #{collector_id} to read as the actual variable collector_id, not the code that I am using to call the variable. Is that possible?
Thanks for the help. I am using ruby 1.9.3.
You can use sub or gsub to replace expected input values:
collector_id = "foo"
test = '#{collector_id}'
test.sub("\#{collector_id}", "#{collector_id}") #=> "foo"
I would avoid the use of eval (or at least sanity check what you are running) to reduce the risk of running arbitrary code you receive from the CSV file.
Try this:
test_to_s = eval("\"#{ test }\"")
puts test_to_s
%q["#{ test }"] will build the string "#{collector_id}" (the double quotes are part of the string, "#{collector_id}".length == 17) which then will be evaluated as ruby code by eval
So I have the following little script to make a file setup for organizing reports that we get.
#This script is to create a file structure for our survey data
require 'fileutils'
f = File.open('CustomerList.txt') or die "Unable to open file..."
a = f.readlines
x = 0
while a[x] != nil
Customer = a[x]
FileUtils.mkdir_p(Customer + "/foo/bar/orders")
FileUtils.mkdir_p(Customer + "/foo/bar/employees")
FileUtils.mkdir_p(Customer + "/foo/bar/comments")
x += 1
end
Everything seems to work before the while, but I keep getting:
'mkdir': Invalid argument - Cust001_JohnJacobSmith(JJS) (Errno::EINVAL)
Which would be the first line from the CustomerList.txt. Do I need to do something to the array entry to be considered a string? Am I mismatching variable types or something?
Thanks in advance.
The following worked for me:
IO.foreach('CustomerList.txt') do |customer|
customer.chomp!
["orders", "employees", "comments"].each do |dir|
FileUtils.mkdir_p("#{customer}/foo/bar/#{dir}")
end
end
with data like so:
$ cat CustomerList.txt
Cust001_JohnJacobSmith(JJS)
Cust003_JohnJacobSmith(JJS)
Cust002_JohnJacobSmith(JJS)
A few things to make it more like the ruby way:
Use blocks when opening a file or iterating through arrays, that way you don't need to worry about closing the file or accessing the array directly.
As noted by #inger, local vars start with lower case, customer.
When you want the value of a variable in a string usign #{} is more rubinic than concatenating with +.
Also note that we took off the trailing newline using chomp! (which changes the var in place, noted by the trailing ! on the method name)