質問

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?

役に立ちましたか?

解決

".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

他のヒント

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top