Using "? return :" - ruby

Below is part of a Ruby function that checks for a specific directory and creates it if it does not already exist:
if Dir.exists?(dir_name) == false
Dir.mkdir(dir_name)
end
I understand that there is a shorter way of doing the exact same thing:
Dir.exists?(dir_name) ? return : Dir.mkdir(dir_name)
However, I can't quite make sense of this. The important part of the second command is ? return :. The first part of the command has the parameter to check, and the last part has the action to take, but I can't make sense of ? return :. If I wanted the action in the last part of the command to execute if and only if dir_name did already exist, what would I use instead of ? return :?

You should use
Dir.mkdir(dir_name) unless Dir.exists?(dir_name)
unless <statement> is the same as if !<statement>. Remember never to make a statement that compares a boolean value to another boolean.
For example, increasing readability of your first statement
if Dir.exists?(dir_name) == false
if !Dir.exists?(dir_name)
unless Dir.exists?(dir_name)
The line Dir.exists?(dir_name) ? return : Dir.mkdir(dir_name) uses the ternary operator.
return immediately exits the function, usually returning a value like return "some value" but you can also just call return to exit the function and return nil.
Long story short the ternary version breaks the function if the dir exists, so nothing after that in the function would happen. So the equivalent is actually
Dir.exists?(dir_name) ? nil : Dir.mkdir(dir_name)

That is another way of writing an if-else.
Condition ? IfTrue : IfFalse
So,
Dir.exists?(dir_name) ? return : Dir.mkdir(dir_name)
Is the same as:
if Dir.exists?(dir_name)
return
else
Dir.mkdir(dir_name)
end

Related

How do the "if and (ne)" operators work in helm?

I'm working with a chart that has the following structure at the beginning but I'm having a hard time understanding when does this evaluate to true.
{{- if and .Values.vpceIngress.enabled .Values.http.paths (ne .Values.vpceIngress.class "traefik2") }}
I believe that both enabled and paths must be true, but the ne throws me off after that.
and and ne are functions in go templates.
and
Returns the boolean AND of its arguments by returning the
first empty argument or the last argument, that is,
"and x y" behaves as "if x then y else x". All the
arguments are evaluated.
ne
Returns the boolean truth of arg1 != arg2
So the template line equates to a more psuedo codish
if (
.Values.vpceIngress.enabled
&& .Values.http.paths
&& .Values.vpceIngress.class != "traefik2"
)
The truthiness of the plain .Value.key is basically a "is this field/key not the zero value for the data type. This also works for maps as 'is this field/key defined' due to a map without that path equating to nil or non value (Note that this only works when the parent map is actually defined! Otherwise the template errors)

Assign a value to a variable on RETURN PL/SQPL

Is there a way to achieve this in PL/SQL oracle ?
RETURN (return_status:=1);
It gives a compilation error when I try to do this. If this is not possible please suggest a better alternative instead of doing
return_status := 1;
RETURN (return_status);
When we execute a RETURN the procedure terminates at that point and control flow passes to the calling program. So there would be no value in this construct …
RETURN (return_status:=1);
… because nothing in the program unit could act on return_status after the RETURN.
this is a function and return_status is an OUT param
That's the root of your problem: poor design. Either return a value or have it as an OUT parameter but not both. The accepted practice in PL/SQL is that a function returns a value and has no OUT parameters. Only procedures (which don't have RETURN) have OUT parameters.
So you choices are:
return 1 and don't have an OUT parameter
set OUT parameter = 1 and return something else
make it a procedure instead
I dont understand what the problem is?
If you want to return the value return_status then the second option is just fine(if you are really doing a hardcoded assignment you could just return 1.
And I thought maybe you actually have an external variable return_status you are trying to change the value of by calling this function. In which case, use a procedure and have return_status be an IN OUT variable(maybe even just OUT).
AFAIK, you cannot assign value in the Oracle FUNCTIONS RETURN Statement.
From Oracle Docs, the syntax should be
RETURN [[(] expression [)]];
For expression refer Expressions
Only solution i can think of based on your requirement(1 condition instead of 2) is using case expression. Something like below (if you have any condition)
RETURN
CASE when return_status is not null
THEN 1
Functions with OUT parameters are permitted but smell wrong.

Ruby returning statement

I am trying to return a UserStubPresenter (a nested JSON presenter) corresponding to the user who received the email, assuming they are still present in the system. If they are no longer present, it should just be nil.
And this is what I have so far for ruby. Please explain this syntax and help me with the return statement in details.
return UserStubPresenter.new #object.recipient if #options[:shallow].to_bool
There are two important pieces to understand. First, Ruby reads this line like this:
return(UserStubPresenter.new(#object.recipient)) if #options[:shallow].to_bool
And it evaluates the if condition first. That means – as zeitnot already wrote – if #options[:shallow].to_bool is truthy then UserStubPresenter.new(#object.recipient) is evaluated and returned.
Seconds, if you don’t return explicitly, then Ruby will return the return value of the last evaluated statement. Your example is a shorter version of
if #options[:shallow].to_bool
return UserStubPresenter.new #object.recipient
else
end
The last expression is the empty else path, therefore, your that line would return nil – no explicit return nil needed.
And assuming that that line is the last line of a method then you do not need the return at all.

Ruby - how to assign multiple variables based on another boolean variable?

I am trying to do:
the_tag= line[2..5]
rec_id_line = (line[2]=='#')? true : false
new_contents,new_to_close=
rec_id_line? open_id_tag(indent,line) : open_tag(indent,the_tag,last_field)
Those two methods both return two values (btw I'm refactoring here)
i.e. for the two variables, I either want to call open_id_tag(2 params) otherwise open_tag(3 params) depending on the true/false rec_id_line value.
You just have to put a space between rec_id_line and ?:
new_contents, new_to_close = rec_id_line ? open_id_tag(indent, line) : open_tag(indent, the_tag, last_field)
Furthermore line[2]=='#' probably returns a boolean value so can simplify your second line:
rec_id_line = (line[2] == '#')
Or combine both lines:
new_contents, new_to_close = (line[2] == '#') ? open_id_tag(indent, line) : open_tag(indent, the_tag, last_field)

How to determine if code is executing as a script or function?

Can you determine at runtime if the executed code is running as a function or a script? If yes, what is the recommended method?
There is another way. nargin(...) gives an error if it is called on a script. The following short function should therefore do what you are asking for:
function result = isFunction(functionHandle)
%
% functionHandle: Can be a handle or string.
% result: Returns true or false.
% Try nargin() to determine if handle is a script:
try
nargin(functionHandle);
result = true;
catch exception
% If exception is as below, it is a script.
if (strcmp(exception.identifier, 'MATLAB:nargin:isScript'))
result = false;
else
% Else re-throw error:
throw(exception);
end
end
It might not be the most pretty way, but it works.
Regards
+1 for a very interesting question.
I can think of a way of determining that. Parse the executed m-file itself and check the first word in the first non-trivial non-comment line. If it's the function keyword, it's a function file. If it's not, it's a script.
Here's a neat one-liner:
strcmp(textread([mfilename '.m'], '%s', 1, 'commentstyle', 'matlab'), 'function')
The resulting value should be 1 if it's a function file, and 0 if it's a script.
Keep in mind that this code needs to be run from the m-file in question, and not from a separate function file, of course. If you want to make a generic function out of that (i.e one that tests any m-file), just pass the desired file name string to textread, like so:
function y = isfunction(x)
y = strcmp(textread([x '.m'], '%s', 1, 'commentstyle', 'matlab'), 'function')
To make this function more robust, you can also add error-handling code that verifies that the m-file actually exists before attempting to textread it.

Resources