Readable vs faster code in function and if statement - performance

function Some()
if (statement1 == "") then
return;
end
if (statement2 == "done") then
-- some
else
-- some
end
end
vs
function Some()
if (statement1 == "") then
return;
elseif (statement2 == "done") then
-- some
else
-- some
end
end
In first case code more readable, in my opinion. But second case may be faster? On not be faster? What do you think?
I'm googling, but I don't know how to formulate a request

Related

How to create a list of variables in ruby?

I’m trying to create something like an inventory in ruby, so I can compare "params" against every line in that inventory, but I’m new to the language and I don’t know what might be the best way to do it.
Actually my code looks like this:
def parseParams(params)
max_length = "xxxxxxxxxxx".length
min_length = 2 #c1 for example
if (params.length == 0)
puts "[-] No parameters provided"
return false
elsif (params.length > max_length)
puts "[-] The parameters are too long/invalid"
return false
elsif (params.length < min_length)
puts "[-] The parameters are too short/invalid"
return false
else
if (params == "c1" || params == "c2" || params == "c3")
puts "[+] Valid parameters"
return true
end
end
end
What I want to do is simplify the code and just verify whether "params" exists in this inventory, otherwise, return error.
Someone knows how to do it?, thanks in advance.
To summarise the requirements of your question:
If params is equal to any line in inventory.txt then it's valid, otherwise it's invalid
You can do this:
def parseParams(params)
File.read('inventory.txt').split("\n").include?(params)
end

What is wrong in this if structures?

I have the method, which shows me the error :
"syntax error, unexpected end-of-input, expecting keyword_end". Where i made a mistake ?
def clicked(p,u)
if (Like.where(post_id: p.id).empty?)
return false
else
posts=Like.where(post_id: p.id)
if ((posts.length<1) && (posts[0].user_id==u.id) && (posts[0].action!=nil))==true then return true
else
if ((posts.length<1) && (posts[0].user_id!=u.id)) then return false
else posts.each do |i|
if (i.user_id==u.id) then return true
end
end
end
end
end
posts.each should be closed by a end keyword
def clicked(p,u)
if (Like.where(post_id: p.id).empty?)
return false
else
posts=Like.where(post_id: p.id)
if ((posts.length<1) && (posts[0].user_id==u.id) && (posts[0].action!=nil))==true then return true
else
if ((posts.length<1) && (posts[0].user_id!=u.id)) then return false
else
posts.each do |i|
if (i.user_id==u.id) then return true
end
# The missing end
end
end
end
end
end
To make it easier for you and others to spot problems of this sort, avoid using the "if....then" form of Ruby's if statement, especially in conjunction with an "else" clause on a separate line. Also, avoid putting code on the same line as the "else" keyword. Instead of this:
if some_condition then do_something
else
do_something_else
end
or this:
if some_condition
do_something
else do_something_else
end
prefer this:
if some_condition
do_something
else
do_something_else
end
Using these fairly standard formatting practices will make this class of syntax errors much easier to avoid, and easier to debug when they do occur.

Getting an "unexpected keyword_end" for if/else statements

def load x
#maze_string = x
#maze_string_split = x.chars.to_a
string_counter = 0
y=#height
x=#width
(0..(y*2+1)).each do |n|
if #maze_string_split[counter] !=1
puts "Error in given string, wall expected"
else
#maze_array[n] = #maze_string_split[counter]
counter++
end
(0..(x*2)).each do |m|
if n==0 || n==(y*2+1) || m==(x*2)
if #maze_string_split[counter] != 1
puts "Error in given string"
else
#maze_array[n][m] = #maze_string_split[counter]
counter++
end
else
#maze_array[n][m] = #maze_string_split[counter]
counter++
end
end
end
end
I am getting the error in the title on the "end" statements at the conclusion of each if/else block. All seems well, but the errors remain. I tried looking to see if anyone else had this problem, but I can't find anything specific to my problem
Ruby does not have a ++ or -- operator.
Ruby will not parse these out correctly in that is the reason you're getting an unexpected keyword_end, it is expecting another operand.
Replace the
counter++ with counter += 1
Also, note that your variable is not called counter but string_counter

problem with if and else code... in ruby

Do not treat to the variables and conditions ...
def index
end
def search
count = 1
while count < 3
if count == 1
#movie = "not found" if #code1 == nil || #code1 == ""
if #movie == ""
end
end
if count == 2
#movie = "not found" if #code1 == nil || #code1 == ""
if #movie == ""
if #code1.include? "movshare"
end
if #code1.include? "novamove"
end
end
end
count++
end
end
end
what is the problem in this code? i get an error:
syntax error, unexpected keyword_end
you have one more unnecessary 'end'. There are 9 opening clauses including def, while and if and 10 closing end
You are confusing the interpreter with your count++. ++ does not exist in Ruby. You need to use count += 1. The interpreter is probably assuming that is an expression involving addition, and expecting another operand but instead finding end

Blocks of statements in Ruby

I have a background from languages which use {} to say that these are "block of statements" but i am learning ruby and really confused how it being done there.
So lets say in C i have
if ( condition )
{
statement1;
statement2;
}
else if (condition)
{
statement1;
statement2;
// nested if
if (condition)
{
dosomethinghere;
}
}
else
{
statement1;
statement2;
}
How i can put this code in ruby? and also mention how to understand synomenous of "{}" in ruby, thanks.
Well, to answer your first question:
if ( condition )
statement1
statement2
elsif (condition)
statement1
statement2
// nested if
if (condition)
dosomethinghere
end
else
statement1
statement2
end
The syntax rule for if statement is:
if expr [then]
expr...
[elsif expr [then]
expr...]...
[else
expr...]
end
Where everything between [] is optional
However, and in other direction, you can also create and pass code blocks, check this post to read more about this topic.
The ruby syntax for if is:
if condition
body
else
alternativa body
end
Or
if condition then body else alternative body end
It's the same for while loops except with do instead of then.
{ and } are used to pass anonymous functions (confusingly called "blocks" in ruby) as arguments to methods.
I'd suggest getting a decent book and sitting down and reading the first few chapters this should cover everything you asked here and a lot more. I'd suggest http://oreilly.com/catalog/9780596529864 although if you're trying to get something done really quick http://www.troubleshooters.com/codecorn/ruby/basictutorial.htm is quite a good brief intro to get you started.
in Ruby, the opening brace is implied after an if. to close the block, you use an end instead of a close brace. the only other difference is you use elsif (condition) instead of else if (condition).
If you're thinking "how do I create a new variable scope in Ruby"? ie:
{
var myvar = 1;
}
myvar = 2; // compile error because myvar isn't in this scope!
I'm not really sure how you would do that.
try running the following:
def example(x,y)
puts "X:#{x},Y:#{y}"
if ( x == 0 ) then
puts "Its true"
elsif (x == 1)
puts "Its not true"
puts "it certainly isn't"
if (y == 0) then
puts "i'm the nested if"
end
else
puts "i made it to the default case"
puts "freedom"
end
puts
end
example(0,0)
example(1,0)
example(1,1)
example(2,2)
If you want a scope you can define your own scope method:
def scope
yield
end
# use like this
scope {
x = 5
puts x #=> 5
}
x #=> undefined local variable
EDIT: for a better approach to 'scopes' in Ruby 1.9 see: http://banisterfiend.wordpress.com/2010/01/07/controlling-object-scope-in-ruby-1-9/

Resources