Simple if else in rails - ruby-on-rails

How to implement simple if else condition in rails
PHP : echo $params = isset($_POST['some_params']) ? $_POST['some_params'] : "";
RAILS : ??
Thanks,

You may want to look into Ruby Ternary operator: A good source is http://www.tutorialspoint.com/ruby/ruby_operators.htm
params[:some_params].present? ? 'a' : 'b'
another way of doing this is:
if params[:some_params].present?
....
else
....
end
There is one more operator called Ternary Operator. This first
evaluates an expression for a true or false value and then execute one
of the two given statements depending upon the result of the
evaluation. The conditional operator has this syntax:

Related

Can a boolean be included in an "if" statement?

I'm just starting out on learning Lua (for 4 days now) and when running this code, I get an error: input:2: 'then' expected near '='
Here's the code I am using:
local imagineVar = true
if imagineVar = true then
print("LOL")
end
How do I fix this?
if imagineVar then
print("LOL")
end
in lua, anything in if statment will be true except false and nil
The error you're getting is a syntax error because assignments (var = something) are statements rather than expressions in Lua - that means they don't evaluate to a value and thus can't be used in an if-condition (or anywhere else where an expression is expected).
As others have pointed out, you'd use the operator == for comparison. It is however more idiomatic to check for truthiness if your variable is a boolean: if imagineVar then ... end; the body of the if will run only if imagineVar is not nil or false.
Compare needs double '='
local imagineVar = true
if imagineVar == true then
print("LOL")
end

Prepend a character if string exists otherwise not

I would like to prepend a '/' if the variable follow has a value otherwise if it is nil then keep it as nil
l2, follow = params[:all].split('/', 2)
follow = follow.nil? ? follow : "/#{follow}"
redirect_to "#{my_path(locale: locale, l2: l2)}#{rest}"
the params[:all] here could be a url path like
esp
esp/article/1
esp/article/1/author/1
EDIT:
My approach works but would like to know if there is a better way
follow.nil? ? follow : "/#{follow}"
Since Ruby has String#prepend method, the code can be refactored the following way:
follow && follow.prepend("/")
Or since Ruby 2.3 has safe navigation, it can be expressed even more concise:
follow&.prepend("/")

what does ? ? mean in ruby [duplicate]

This question already has answers here:
How do I use the conditional operator (? :) in Ruby?
(7 answers)
Closed 7 years ago.
What does the line below checks and perform?
prefix = root_dir.nil? ? nil : File.join(root_dir, '/')
Here is the block that contains the line of code.
def some_name(root_dir = nil, environment = 'stage', branch)
prefix = root_dir.nil? ? nil : File.join(root_dir, '/')
.
.
.
i know that the '?' in ruby is something that checks the yes/no fulfillment. But I am not very clear on its usage/syntax in the above block of code.
Functions that end with ? in Ruby are functions that only return a boolean, that is, true, or false.
When you write a function that can only return true or false, you should end the function name with a question mark.
The example you gave shows a ternary statement, which is a one-line if-statement. .nil? is a boolean function that returns true if the value is nil and false if it is not. It first checks if the function is true, or false. Then performs an if/else to assign the value (if the .nil? function returns true, it gets nil as value, else it gets the File.join(root_dir, '/') as value.
It can be rewritten like so:
if root_dir.nil?
prefix = nil
else
prefix = File.join(root_dir, '/')
end
This is called a ternary operator and is used as a type of shorthands for if/else statements. It follows the following format
statement_to_evaluate ? true_results_do_this : else_do_this
A lot of times this will be used for very short or simple if/else statements. You will see this type of syntax is a bunch of different languages that are based on C.
The code is the equivalent of:
if root_dir.nil?
prefix = nil
else
prefix = File.join(root_dir, '/')
end
See a previous question

How to Build regular expression pattern in rails

I am actually writing rails code where i want to check if
params[:name] = any character like = , / \
to return true or return false otherwise.
How do i build a regex pattern for this or if any other better way exists would help too .
sanitized = params[:name].scan(/[=,\/\\]/)
if sanitized.empty?
# No such character in params[:name]
else
# oops, found atleast 1
end
HTH
I don't know if it's achieved the status of "idiomatic", but I think the most compact way of achieving this in Ruby is with double !:
!!(params[:name] =~ /[=,\/\\]/)
as discussed in How to return a boolean value from a regex

Scala Parser, set reserved words

I am writing a simple proggramming language with scala parser. So far no trouble, but im worrying about the relation function name / variable name against reserved words.
I'va already addded some special functions like "floor" ~ gexp or "top" ~ gexp and i dont want anybody using this language being able to name a function or a variable like them. I have not found yet a way to check this.
in Ruby i would write something like
rule varname
lowerid &{ |id| id[0].is_not_reserved } <VarNameNode>
but i dont know how would i write this in scala
def varName : Parser[StringValue] = lowerid
You can use the ^? operator:
def varName: Parser[StringValue] = lowerid ^? ({
case id if !isReserved(id) => id
}, { id => s"Error: $id is reserved." })

Resources