1

I am wondering if it is possible, given that these global variables exist and are accessed by other functions, to tell a function to use one variable or another without re-writing huge chunks of code. I have to use global variables because of my limitations with calabash passing variables around (correct me if I'm wrong). I want to be able to call one function that can grab information and stuff it into one of several global variables. Something hopefully like this:

def add_five(my_var)

   if my_var == "my_cats"
      use $my_cats for my_var_nickname
   elsif my_var == "my_dogs"
      use $my_dogs for my_var_nickname
   elsif my_var == "my_birds"
      use $my_birds for my_var_nickname
   end

   my_var_nickname = my_var_nickname + 5
end

$my_cats = 2
$my_dogs = 3
$my_birds = 3

add_five("my_cats")

$my_cats = 7
$my_dogs = 3
$my_birds = 3

Thanks for the help!

4
  • 1
    You could write something like b = ((stuffy == "foo") ? $A : $B) and then use b, but you should try to avoid using global variables. Commented Oct 22, 2014 at 19:42
  • 2
    Using globals ($) like that has significant code smell. Commented Oct 22, 2014 at 20:19
  • I unfortunately don't have much choice when using calabash. I need a way to record and communicate data back and forth between test steps. Commented Oct 22, 2014 at 20:21
  • Try reading this ryanverner.com/post/33837637667/… Commented Oct 24, 2014 at 5:05

3 Answers 3

1

I second the point that using global variables has a foul smell to it...

Anyway, you could try using a hash:

$animal_counts = {}
def add_five(animal_type)
  $animal_counts[animal_type] += 5
end

$animal_counts[:cats]  = 2
$animal_counts[:dogs]  = 3
$animal_counts[:birds] = 3

add_five(:cats)

$animal_counts[:cats]  # => 7
$animal_counts[:dogs]  # => 3
$animal_counts[:birds] # => 3

Hope that helps!

Sign up to request clarification or add additional context in comments.

1 Comment

Hmm that is interesting. In my actual case, the globals are already hashes themselves... I'll have to look into this one more.
0
def record_as(stuff)
   variable_to_use = if stuff == "foo"
      $variableA
   elsif stuff == "bar"
      $variableB
   end

   #do stuff with variable_to_use
end

5 Comments

Will that save it back in the original variable or is that just making a copy of it to use in that function?
This creates a new reference to the variable, so editing the new reference will reflect on the global
While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the questions for readers in the future, and those people might not know the reasons for your code suggestion.
I tried it, and the information never made it back out to the global variable. It could have something to do with the fact that it's in the middle of a calabash step too though.
We don't use variableA for variable names in Ruby by convention, whether they're global or not. Instead, use snake_case, variable_a.
0

For simple use In your ruby step file define a variable like this

$global_memory=""

In your calabash step which is ok to be in another ruby step file. Read some text from query and store it on that variable in run time.

Then I should memorize delivery time

Which define in ruby step file like this

Then /^I should memorize delivery time$/ do
  srctext=query("label marked:'lblOrderTime'",:text).first
  srctext=srctext.to_s
  $global_memory.replace(srctext)
end

Later in the scenario you can recall that value and use it like this

Then I should see order dispatch time same as in checkout screen and receipt screen

Which define in ruby step file like this

Then /^I should see order dispatch time same as in checkout screen and receipt screen$/ do |arg|
  datetext=$global_memory
    result = query("webView css:'SPAN' index:5").first["textContent"].include? "#{datetext}"
    unless result
      textFound = query("webView css:'TABLE' index:0").first["textContent"]
      screenshot_and_raise "time  #{datetext} not found instead #{textFound} is found"
    end 
end

Further you can use Hash (Key value pairs) to store many variable at same time and retrieve them.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.