if stament always returns false - cocoa

I'm unable to get this function to work. The if always return false. What i'm doing wrong?
This come from a cocoa checkbox, the OverrideBuildCheked is 1 when checked and 0 when unchecked.
on OverRideBuildNumber_(sender)
set OverrideBuildCheked to CurrentBuildOverrideChk's state
if OverrideBuildCheked = 1 then
CurrentBuildField's setEnabled_(true)
CurrentBuildField's setEditable_(false)
else
CurrentBuildField's setEnabled_(false)
CurrentBuildField's setEditable_(true)
end if
end OverRideBuildNumber_

Just about everything from Cocoa is a pointer to an object (you can see this by logging the result), so you usually need to coerce to the desired AppleScript class, e.g. CurrentBuildOverrideChk's state as integer (or boolean).

Related

Variable assignment inside of ruby methods

I have this pretty simple method:
def update_context(msg, session, sender)
previous_context = session.context
session.update(context: intent_determination(msg, session.context, sender))
session.update(context: brand_determination(msg, session.context))
session.update(context: style_determination(msg, session.context))
session.update(context: price_range_determination(msg, session.context))
session.update(context: size_determination(msg, session.context))
p previous_context
p session.context
p (previous_context == session.context)
unless session.context.size == 0
if previous_context == session.context
session.context["intent"] = "lost"
session.save
end
end
end
My problem is certainly due to a stupid mistake I can't see but please bear with me on this one, I really can't see it.
As you can see, I "save" the session's context in a previous_context variable at the beginning of the method. Then, I'm running a few updates on the context.
However, when I print previous_context, session.context and previous_context == session.context, I get the same result for the first two, and true for the last one.
How is this possible ? I assigned to previous_context the value of session.context before updating it. And then, previous_context has the same value as session.context after I've updated it.
I really can't see where I screwed up here, or there is definitely something I don't understand.
previous_context = session.context makes the previous_context variable point to the same object as session.context. If you want to change one without affecting the other, you'll need to create a copy of session.context to store in previous_context.
in Ruby, variables are just references to objects, so what you are doing there is merely creating a new reference to the same object. If you wish to save the previous state you will have to copy the entire object.
See this answer for a more graphic explanation.

Why Is This Ruby While Statement Not Working

Background: This is using page-object, and Rspec. The page, objects are setup correctly. Basically what is happening is its skipping the while statement and going directly to checking of the variable label exists on the page.
What it SHOULD do is check if the label variable is on the page and if not then wait 20 seconds and refresh the page then check again for up to 30 times and if it fails on the 30th time then it should fail all together. If it finds it then it should check again and pass it.
def check_label(label, current_page)
labels_filter_element.click
check_label_sla(label, current_page)
current_page.text.should include label
end
def check_label_sla(label, current_page)
t = 30
i = 0
while i < t
if current_page.text.should include label
return
else
sleep(20)
current_page.refresh
i += 1
end
end
end
I think what is happening is if current_page.text.should include label is always returning TRUE, but I'm not sure. If more info is needed please let me know.
I am making the following assumptions:
The behaviour you are seeing is that the page is first loaded, the labels_filter_element is clicked, the label is not on the page and the test fails immediately without refreshing the page.
label is a string
Given these assumptions, the problem is with the if-statement:
if current_page.text.should include label
When the if statement is executed, it will assert that the page includes the label:
If the page has the label, the code in the if will be executed (ie it will immediately return and the assertion in check_label will pass).
In the case that the page does not have the label, the assertion will throw an exception causing the test to fail immediately (ie the else code will not get executed). So the test is failing inside the check_label_sla method, not at the end of the check_label method (which makes it seem like the while loop is not working).
You want the if statement to check that the text is included, without making an assertion. Therefore you want:
if current_page.text.include? label
So your check_label_sla method would become:
def check_label_sla(label, current_page)
t = 30
i = 0
while i < t
if current_page.text.include? label
return
else
sleep(20)
current_page.refresh
i += 1
end
end
end
Or you could reduce it to:
def check_label_sla(label, current_page)
30.times do
return if current_page.text.include? label
sleep(20)
current_page.refresh
end
end

VB6 how to get the selected/checked control in a control array

I have to modify a VB6 app and am repeatedly beating my head against a wall over control arrays.
I know that the event handler for the array includes its index value and I can set some variable there, but i should be able to directly access the selected radio button in an array of OptionButton. Currently I'm doing this
For i = 0 To optView.Count - 1
If optView.Item(i).value = True Then
currIndex = i
Exit For
End If
Next
Is this really my only option?
Yes, this is our only option. The control array object does not contain any selecting logic (which makes sense, as "selected" might mean different things for different controls). The only change I'd make is replacing the For with For Each.
Another way to do this that I have used. Write a function, and then call the function, passing in the control name, to return the index number. Then you can reuse this in the future especially, if you add it to a module (.bas).
Function f_GetOptionFromControlArray(opts As Object) As Integer
' From http://support.microsoft.com/KB/147673
' This function can be called like this:
' myVariable = f_GetOptionFromControlArray(optMyButtons) 'Control syntax OK
' myVariable = f_GetOptionFromControlArray(optMyButtons()) 'Array syntax OK
On Error GoTo GetOptionFail
Dim opt As OptionButton
For Each opt In opts
If opt.Value Then
f_GetOptionFromControlArray = opt.Index
Exit Function
End If
Next
GetOptionFail:
f_GetOptionFromControlArray = -1
End Function

Is Nothing comparison gives type mismatch

I am trying to check if the 'Listivew.Tag property is nothing'.
I used to do the 'Is Nothing' check universally for all scenarios as first check to avoid errors
Can someone explain how to do it in VB 6?
If Not .lvwLocation.Tag Is Nothing Then
'COMPANY
str = str & IIf(Len(.lvwLocation.Tag) > 0, " and u.location_id in " & .lvwLocation.Tag, "")
End If
Gives error 'type-mismatch'
Nothing is a valid value for Object variables, and Is is the way to compare object pointers.
But a VB6 control's Tag property is a String, and VB6's String type is not an Object; it's a primitive type. That means a String variable can't be assigned Nothing -- its emptiest possible value is the empty string. (And an Object variable can't be assigned a String value.) For strings just use the same equality/inequality/comparision operators that you use for other primitive (numeric/boolean/date) types:
If .lvwLocation.Tag <> "" Then ...
In VB6 it appears that using Is Nothing to compare Objects works, Every other data type that I tried did not. In .Net Nothing represents the default value of any data type and will work like you expect.
Dim test as Object
If Not test Is Nothing Then
/////
End If
Since it appears the data type of th Tag property in VB6 is a string. I would use something like:
If .lvwLocation.Tag <> "" Then
/////
End If

Is 'buggy_logger' a reference to the 'status' string in this Ruby example?

In this example, do the nukes get launched because any changes that you make to buggy_logger get applied to the 'status' string - just like using a copy of a reference to an object -> when you make a change to the copy of the reference, the change gets applied to the underlying object -> that change is, in turn, reflected in any other references to the object. So, in other words, buggy_logger is an alias to the 'status' object without specifically using the alias keyword? Is that correct? So, in ruby, you just say
b = a
and then any changes you make to b afterwards are also reflected in a. Or is this only true because we're talking about Strings, which are mutable in Ruby?
# example-4.rb
status = "peace"
buggy_logger = status
print "Status: "
print buggy_logger << "\n" # <- This insertion is the bug.
def launch_nukes?(status)
unless status == 'peace'
return true
else
return false
end
end
print "Nukes Launched: #{launch_nukes?(status)}\n"
# => Status: peace
# => Nukes Launched: true
Yes, it is because strings are objects. Try
buggy_logger = status.dup
If you want a distinct object with the same initial value.
As for your question about alias I suspect you aren't correctly understanding how alias is used in ruby; it's used on methods, not objects, and isn't related to mutability.
Note also that the same semantics would have applied with any class; if status had been an array, a file, or anything else (provided it had mutable state suitable for use as a logger), you would have gotten analogous results.
One warning about dup though. If your object refers to other objects, the copy will also refer to the same objects. It's fine once you start thinking about it the right way, but tricky till then.

Resources