extract data from variable in ruby on rails - ruby

I have a variable in ruby on rails which contains this value 8,375 TND
_result = _orig_account.formatted_balance(_currency)
I want if it is possible to eliminate TND and then multiply the rest by 1000 in order to have as a result 8375
can sameone help me to solve this problem
I try with this code :
I try with : _tes = _orig_account.formatted_balance().tr(',', '.').to_f
_te = _tes * 1000
and I have as a result : 8375.0
but as I said I want juste 8375

UPDATE: Just use this '8,375 TND'.tr(',', '').to_i

It's a simple manipulation:
'8,375 TND'.tr(',', '.').to_f
=> 8.375
Multiply that by 1000.

Related

Is there a way to use range with Z3ints in z3py?

I'm relatively new to Z3 and experimenting with it in python. I've coded a program which returns the order in which different actions is performed, represented with a number. Z3 returns an integer representing the second the action starts.
Now I want to look at the model and see if there is an instance of time where nothing happens. To do this I made a list with only 0's and I want to change the index at the times where each action is being executed, to 1. For instance, if an action start at the 5th second and takes 8 seconds to be executed, the index 5 to 12 would be set to 1. Doing this with all the actions and then look for 0's in the list would hopefully give me the instances where nothing happens.
The problem is: I would like to write something like this for coding the problem
list_for_check = [0]*total_time
m = s.model()
for action in actions:
for index in range(m.evaluate(action.number) , m.evaluate(action.number) + action.time_it_takes):
list_for_check[index] = 1
But I get the error:
'IntNumRef' object cannot be interpreted as an integer
I've understood that Z3 isn't returning normal ints or bools in their models, but writing
if m.evaluate(action.boolean):
works, so I'm assuming the if is overwritten in a way, but this doesn't seem to be the case with range. So my question is: Is there a way to use range with Z3 ints? Or is there another way to do this?
The problem might also be that action.time_it_takes is an integer and adding a Z3int with a "normal" int doesn't work. (Done in the second part of the range).
I've also tried using int(m.evaluate(action.number)), but it doesn't work.
Thanks in advance :)
When you call evaluate it returns an IntNumRef, which is an internal z3 representation of an integer number inside z3. You need to call as_long() method of it to convert it to a Python number. Here's an example:
from z3 import *
s = Solver()
a = Int('a')
s.add(a > 4);
s.add(a < 7);
if s.check() == sat:
m = s.model()
print("a is %s" % m.evaluate(a))
print("Iterating from a to a+5:")
av = m.evaluate(a).as_long()
for index in range(av, av + 5):
print(index)
When I run this, I get:
a is 5
Iterating from a to a+5:
5
6
7
8
9
which is exactly what you're trying to achieve.
The method as_long() is defined here. Note that there are similar conversion functions from bit-vectors and rationals as well. You can search the z3py api using the interface at: https://z3prover.github.io/api/html/namespacez3py.html

GAMS decimal numbers

I have the following code:
loop (d,
rnd(d)=uniformInt(1,nd)
);
I am going to use the integer numbers rnd(d) as an index of another set s(i). But for example when rnd(d)=34.000 however, it is integer, but s(34.000) has no valid index since, 34.000 is not 34 !! and GAMS shows a error message.
I don't know if #Lutz solution worked out for you. In case it did not, you can try the following:
First, it is not necessary to loop over the set d, just a simple:
rnd(d) = uniformInt(1,nd);
will suffice.
Next line can go like:
loop(d,
s(i)$(i.val = ord(d)) = . . .;
);
If you still have issues, then using #Lutz suggestion just append '*1.000' to 'ord(i)' and/or 'rnd(d)', whichever is giving you issues.
Is i an ordered set? If yes, you could use something like this:
loop(d,
s(i)$(ord(i)=rnd(d)) = ...;
)

Assigning values to different variable in JMeter

I have two different array with values as below:
Code = [8,9,10]
Value = [4,5,6]
I need to get the values from each array (above mentioned) randomly and assign it to different variable like below:
Code 1 = 9 , Code2=10
Value1 = 4 , Value2=6
Or is there any way in Jmeter to Pass that array to another sampler thereby assigning it to different variables.
How can we achieve it on Jmeter ? Any help / Suggestions is welcome!
Your values look utterly like JSON Arrays so my expectation is that you could handle it more easy using JSON Extractor
Just in case I'm wrong you can get random code and/or value using the following Groovy code in any of JSR223 Test Elements
import org.apache.commons.lang3.RandomUtils
def codes = vars.get('Code').findAll(/\d+/ )*.toInteger()
def values = vars.get('Value').findAll(/\d+/ )*.toInteger()
def randomCode = codes.get(RandomUtils.nextInt(0,codes.size()))
def randomValue = values.get(RandomUtils.nextInt(0,values.size()))
log.info('Random code: ' + randomCode)
log.info('Random value: ' + randomValue)
Demo:
You can use "Config Element" > "Random Variable" where you can give a range and ask for a random number within that given range.
Hope it helps.

Division on rails 4

I'm having a hard time on thinking what should I do with this problem. I'm dividing the two numbers and expecting a non-whole number answer. Meaning to say it should be on decimal format. But unfortunately it answers a whole number.
example: 5 / 2 = 2
s.apts = sum_pts.to_f / sum_game.to_f
You just need to tell Ruby you are doing non-integer division, by writing the problem as "5.0 / 2.0"
See:
Why is division in Ruby returning an integer instead of decimal value?
Try this:
a = 5.0
b = 2.0
puts a / b

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}

Resources