Behaviour on html_safe? in Rails 3.2.1 - ruby-on-rails-3

I am trying to use the html_safe? method to check if a string/text which is retrieved from the DB contains any HTML. However, the html_safe? method is not returning the value I expected. Let's look at the following examples:
>> 'this is a string'.html_safe?
false
>> '<strong>this is a string</strong>'.html_safe?
false
I would expect the first invocation of html_safe? to return true, and the second invocation to return false. The results on the Rails console return false on both of the invocations...
Am I misunderstanding the purpose of html_safe? or there is a better way of doing what I want to achieve?

After more digging, the html_safe? method only return a boolean value that to indicate whether or not the type/object itself is html safe.
Therefore, it doesn't really matter what the contents are within a string, the html_safe? method would also return false for String type.

Related

How to use ABAP boolean in IF condition?

I know abap has no real boolean type. Instead 'X' and ' ' is used. Up to this time I always used an if-statement that way:
IF myObj->is_sth( ) = abap_true.
ENDIF.
Now I did something like this:
IF myObj->is_sth( ).
ENDIF.
And I'm wondering that this seems to work. Return Type is boolean. I'm on Netweaver 7.4. Can I use this without problems? It's like my lovely C# writing :p.
This is called a predicative method call:
A predicative method call is a relational expression whose only
operand is a functional method call meth( ... ). The result of the
relational expression is true if the result of the functional method
call is not initial and false if the result of the functional method
call is initial. The result of the functional method call (the return
value of the called function method) can have any data type. A check
is made on the type-friendly initial value.
A predicative method call, like any relational expression, can be a
full logical expression or part of a logical expression. This means it
can be specified as a condition in control statements and other
statements, as an argument in Boolean functions or conditional
expressions, or in joins with Boolean operators.
This was introduced in 7.40 SP08. Be aware that this only works reliably if the initial value is false and false is the initial value. For instance, IS-H uses a character field where 0 is false and 1 is true - but since the initial value of a character field is a space, that's neither true nor false, so using any method that returns this value will always branch as if the method had returned true...

How to get a boolean indicating if a search is using regex?

I know that the methods instance.search() and instance.column(1).search() are returning a search string. But, how can I know if they should be triggered like a regex? I mean, regex search can be activated by either using
$("something").DataTable({
"search": {
"regex": true
}
});
or by activating the regex flag in the .search() method. But even then, the above named methods return a string, not a RegExp.
So when calling one of the above methods, how am I able to know if they should be handled like a RegExp?

Boolean for API Get request, with default. Y/N/All or true/false/all

I'm implementing a filter for a boolean value, but I want to have a default value, so for example.
parameter omitted - returns where isPublished=true
isPublished=true - return's where isPublished=true
isPublished=false - return's where isPublished=false
What If I want to return everything? I could do isPublished=all but some have complained that this is confusing as its not then a true boolean.
I could also go with Y/N/All or Either or Both
What are others views?
Instead of a boolean that restricts the values to true and false, you could consider using an enumeration and a parameter called status or something similar to send its value to the server:
status = PUBLISHED | NOT_PUBLISHED | ALL
If the status parameter is ommited, assume its value is PUBLISHED. Otherwise, use the value provided in the parameter.
In the long run, using an enumeration will give you the possibility of expanding the available status.

Object of class Codeception\Maybe could not be converted to int

I'm checking out Codeception, and I'm trying to write my own grabber.
In my WebHelper.php:
function grabMaxOffers()
{
return 10;
}
(note: eventually, this will return a dynamic value)
In my TestCept.php file:
$max = $I->grabMaxOffers();
$I->wantToTest("Maximum offers ($max)");
There error I always get is:
PHP Notice: Object of class Codeception\Maybe could not be converted to int in tests/acceptance/TestCept.php on line 21
What am I missing? I wrote two other grabbers (returning strings) that worked fine.
You should not pass parameters into wantTo. wantTo is test name, nothing more. That's why test is failing.
Codeception uses Maybe proxy object while analyzing the test. It mocks strings, array, or anything else. But it should be implicitly converted to string with (string)$max.
Probably you wanted to use comments, and not wantTo statement. You should try amGoingTo method.
Cheers

Optional ByVal boolean parameter is not taking its default value

I am debugging a project right now and it has a function with the following signature:
Public Function changeRemoteDirectory(ByVal newDirectory As String, Optional ByVal Direction As Boolean = True) As Boolean
MsgBox(direction)
'rest of code
End Function
I was trying to figure out what was causing this function to return a value of False when I knew that it should return True given the input that I was supplying, so I put MsgBox(direction) into the Function to see what the value of direction was when I called the Function. I called the function like this, yet I received a MsgBox that showed the value of direction to be False:
changeRemoteDirectory("/internal")
The first parameter works just fine and the code that requires it to execute works correctly, but I cannot figure out why Direction has a value of False in a situation where, I believe, it should have its default value of True. I'm not totally opposed to rewriting the Function if need be, but can anybody here discern why Direction does not have a value of True when the function changeRemoteDirectory() is called without the second parameter supplied?
It sounds like you're experiencing one of many pains that optional parameters cause.
When the code that calls changeRemoteDirectory is compiled, the optional parameter is injected into the calls just like a const would be replaced at compile time. Changes to the value of an optional parameter will not take effect without recompiling the caller.
See this article for more info.
In general, you should use method overloads instead of optional parameters. You get all of the same functionality without the pain and performance drawbacks.