how to increase count by 1 in ruby? - ruby

I am using the below code to get the number of records.
self.size.to_s
How can i add '1' to the size from above?

Integer#next:
self.size.next.to_s

(self.size + 1).to_s # Like this

You can either add it before converting to string
(self.size + 1).to_s
Or if you already have a string
"2".to_i + 1
Combined this could be something like
self.size.to_s.to_i + 1
but that is hardly practical

Related

In a series of IF - ELSE IF statements, only the first IF works

I am trying to create a New variable "X", from Variables "A" "B" and "C".
This is the code that I am using.
I only get the first option "VAS_USAGE=1", the other options are lost.
What I wanted was for VAS_USAGE to have 1 to 6 values.
What am I doing wrong?
```
compute VAS_USAGE=0.
DO IF ((VAS1=1) AND (VAS2=1) AND (VAS3=1)).
COMPUTE VAS_USAGE=1.
ELSE IF ((VAS1=1) AND (VAS2=1) AND (VAS3=0)).
COMPUTE VAS_USAGE=2.
ELSE IF ((VAS1=1) AND (VAS2=0) AND (VAS3=1)).
COMPUTE VAS_USAGE=3.
ELSE IF ((VAS1=1) AND (VAS2=0) AND (VAS3=0)).
COMPUTE VAS_USAGE=4.
ELSE IF ((VAS1=0) AND (VAS2=0) AND (VAS3=1)).
COMPUTE VAS_USAGE=5.
ELSE IF ((VAS1=0) AND (VAS2=0) AND (VAS3=0)).
COMPUTE VAS_USAGE=6.
END IF.
EXECUTE.```
This is how the nested table looks like. This is what I was expecting to get.
This is what I got.
1.00 = 63
As suggested by #user45392 I would check if the answers 'yes' and 'no' are indeed coded as 1 and 0.
In any case I would suggest avoiding the complex 'do if' scheme which might be prone to errors and harder to debug. For example, you could create VAS_USAGE like this:
compute VAS_USAGE = 100*VAS1 + 10*VAS2 + VAS3.
Or if you want to stick to the specific values you gave in your post, add this:
recode VAS_USAGE (111=1)(110=2)(101=3)(100=4)(1=5)(0=6).
Also if you just want a simple index from 1 to 8 you can do this:
compute VAS_USAGE = 4*VAS1 + 2*VAS2 + VAS3 + 1.

Populate an array in ruby with 7n +1

How do I make the following array programatically in Ruby (1.9).
It follows the pattern 7n + 1 and I'd like it to contain 24 numbers.
arr = ["8","15","22","29","36","43","50","57","64","71" ]
Use collect and apply to_s on the result:
(1..24).collect{|n| (n*7 + 1).to_s}
EDIT: sorry forgot to convert numbers to strings. Code is edited now.
Array.new(24){|i| (i * 7 + 8).to_s}

Recursively counting the number of characters in a string. (Ruby)

I need to write a recursive function that utilizes just two string methods, .empty? and .chop.
No, I can't use .length (Can you tell it's homework yet?)
So far I'm stuck on writing the function itself, I passed it the string, but I am unsure on how to recursively go through the characters with the .chop string method. Would I just have a counter? Syntax for this thing seems tricky to me.
def stringLength(string)
if string.empty?
return 0
else
.....
end
end
I wish I could put more down, but this is what I'm stuck at.
return 1 + stringLength(string.chop)
Thats your missing line. Here is an example of how this will work:
stringLength("Hello") = 1 + stringLength("Hell")
stringLength("Hell") = 1 + stringLength("Hel")
stringLength("Hel") = 1 + stringLength("He")
stringLength("He") = 1 + stringLength("H")
stringLength("H") = 1 + stringLength("")
stringLength("") = 0

How do I create an object that will have a number at the end that will increment each time it is used?

I am trying to make a variable that I can increment each time after I use it.
The $companyLevel os the variable that I need to increment.
count = 20
# Variables (20)
while count > 0
$levelName = ""; 8.times{$levelName << (65 + rand(25)).chr}
$companyLevel = "CLev5"
browser2.button(:id, "addCompanyLevel").click
sleep 2
browser2.text_field(:id, $companyLevel).set $levelName
$companyLevel += 1
count -= 1
end
How do I create a variable that will have a number at the end that will increment each time it is used?
Thanks.
Since you already have a count, why does this need to be a variable? why not just do simple string concatenation to create the value you want on the fly
companyLevel = "CLev" + count.to_s
Unless you need to perhaps read up on what an 'array' is?
I'd suggest you purchase and read the book "Everyday Scripting with Ruby" it's a great way to lean the basics of the ruby language and geared towards testers.
This is achieved by creating an object with a property that increments not by creating a variable that increments.

how does Enumerable#cycle work? (ruby)

looper = (0..3).cycle
20.times { puts looper.next }
can I somehow find the next of 3? I mean if I can get .next of any particular element at any given time. Not just display loop that starts with the first element.
UPDATE
Of course I went though ruby doc before posting my question. But I did not find answer there ...
UPDATE2
input
looper = (0..max_cycle).cycle
max_cycle = variable that can be different every time the script runs
looper = variable that is always from interval (0..max_cycle) but the current value when the script starts could be any. It is based on Time.now.hour
output
I want to know .next value of looper at any time during the running time of the script
Your question is not very clear. Maybe you want something like this?
(current_value + 1) % (max_cycle + 1)
If, for example, max_cycle = 3 you will have the following output:
current_value returns
0 1
1 2
2 3
3 0
http://ruby-doc.org/core-1.9/classes/Enumerable.html#M003074

Resources