Question

I am new to programming and Ruby both. In some existing code, it says something like this:

kid_raising_btn=query("switch marked:'KIDRAISING'",:isOn).first

From this, I understand that a variable kid_raising_btn is defined, which will query and return true or false, then call the method first (but this is confusing).

How can I find what first does?

Was it helpful?

Solution

".first" is kind of convenient method. Answer for the UI query comes as an array. So the ".first" represent the first element of the array. There are few methods like ".count"

Ex: your Query :

ans = query("button",:accessibilityLabel)

Assume You will get result like this

[
    [0] "icon rewards new",
    [1] "icon my receipts new",
    [2] "icon my account",
    [3] "icon order@2x",
    [4] "icon check in"
]

if you use ".first" like this

ans2 = query("button",:accessibilityLabel).first

you will get a String with first element as a result instead of an array.

"icon rewards new"

Now you can see you get the first element as the answer

OTHER TIPS

From your description, it sounds like the method chaining here might be contributing to your confusion, so first let's rewrite that line of code like this:



    query_response = query("switch marked: 'KIDRAISING'", :isOn)
    kid_raising_btn = query_response.first

Now, it's easier to see that the #query method returns an object that has a #first method. You can take a look at this object to see what class it is (query_response.class), and then either look up the appropriate documentation for the class or find the method definition in your codebase. For example, if #query returns an array, you can find the documentation here: http://ruby-doc.org/core-2.0.0/Array.html#method-i-first

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top