Blocks of statements in Ruby - 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/

Related

How to convert this if condition to unless?

if array.present?
puts "hello"
end
There is no else part to this.
How to write the above if condition using unless.
I'm asking this question because of this lint error:
Use a guard clause instead of wrapping the code inside a conditional expression
Regarding your comment:
I'm asking this question because of this lint error
Use a guard clause instead of wrapping the code inside a conditional expression
This means that instead of:
def foo(array)
if array.present?
puts "hello"
end
end
You are supposed to use:
def foo(array)
return unless array.present?
puts "hello"
end
See https://github.com/bbatsov/ruby-style-guide#no-nested-conditionals
If this is a Rails question (is it?), you can also use blank?:
def foo(array)
return if array.blank?
puts "hello"
end
There's no reason to.
Remember: unless is the inverse of if (or !if if you rather), and is only intended to make your code easier to read.
Using unless with your expression would be incredibly awkward, because you're now moving the actual body of work to an else statement...
unless array.present?
return
else
puts "hello"
end
...which doesn't make your code any easier to read if you had stuck with a negated if:
if !array.present?
return
else
puts "hello"
end
Don't use unless here. You lose readability in exchange for virtually nothing.
One-liner:
puts "hello" unless !array.present?
However, I would recommend:
puts "hello" if array.present?
unless array.present?
return
else
puts "hello"
end
OP requested one-liner modification:
Pseudocode:
something unless condition
Therefore:
puts "hello" unless !array.present?

Return from a loop from a different method

My understanding is you can exit from a program with a return. How do you return from a loop? When I run return_method as in the following, I want to exit the loop with "RETURNING" returned.
def return_method
return "RETURNING"
end
loop do
puts "Enter:"
answer = gets.chomp
if answer == 'run'
return_method
end
break if answer == 'y'
end
break doesn't work within my method.
A typical way to escape from nested loops of from nested method calls is to use catch ... throw.
RETURNING = "RETURNING"
def return_method
throw RETURNING
end
catch(RETURNING) do
loop do
puts "Enter:"
answer = gets.chomp
if answer == 'run'
return_method
end
break if answer == 'y'
end
end
It's generally not the case that a method call forces the calling method to do something as abrupt as return. That's what exceptions are for, they will bubble up if not caught, but that's seriously heavy-handed for this sort of thing. Instead make your method return a truthy value if you want to break the loop:
def return_method
puts "RETURNING"
true
end
loop do
puts "Enter:"
answer = gets.chomp
case (answer)
when 'run'
break if return_method
when 'y'
break
end
end

How can I make script work in Ruby?

I am new to Ruby.
I need to make this script work:
puts "Do you like cats?"
ask = gets
def ask(n)
if ask == yes
return "I do too"
end
if ask == no
return "Dogs are better"
end
end
puts "#{ask(n)}"
Error message is :
pracif.rb:15:in <main>': undefined local variable or methodn' for
main: Object (NameError)
Here's a script that would work for you :
puts "Do you like cats?"
answer = gets
def ask(n)
if n == 'yes'
return "I do too"
end
if n == 'no'
return "Dogs are better"
end
end
puts ask(answer.downcase.chomp)
Explaination
As the error said you were trying to pass in a variable n which was not defined
Secondly you have a method name ask same as variable name. I've renamed the variable to answer instead
Thirdly, enclose yes and no in quotes
And finally, since you are using gets a \n gets appended like yes\n so none of your conditions would match. So i've used chomp to remove \n. And also used downcase to make input case insensitive.
EDIT
As mentioned by #Jordan in the comments, there is no reason to use string interpolation for the puts statement. So it's enough to call the method directly.
There are a bunch of issues with your code. Try something more like:
def reply(response)
return 'I do too' if response == 'yes'
return 'Dogs are better' if response == 'no'
'Invalid response!'
end
puts 'Do you like cats?'
response = gets().chomp()
puts reply(response)
Pay attention to the variable names. If you keep them descriptive, it is easier to spot mistakes.
Your script has no n local variable defined that you are passing to your ask(n) method at the end.
Rename your ask variable that your script gets from user to answer for example and pass it to your ask method at the end like so:
Updated code to fix other problem I did not see in the first run.
puts "Do you like cats?"
answer = gets.chomp
def ask(n)
(n == 'yes') ? "I do too" : "Dogs are better"
end
puts "#{ask(answer)}"

what is the point of return in Ruby?

What is the difference between return and just putting a variable such as the following:
no return
def write_code(number_of_errors)
if number_of_errors > 1
mood = "Ask me later"
else
mood = "No Problem"
end
mood
end
return
def write_code(number_of_errors)
if number_of_errors > 1
mood = "Ask me later"
else
mood = puts "No Problem"
end
return mood
end
return allows you to break out early:
def write_code(number_of_errors)
return "No problem" if number_of_errors == 0
badness = compute_badness(number_of_errors)
"WHAT?! Badness = #{badness}."
end
If number_of_errors == 0, then "No problem" will be returned immediately. At the end of a method, though, it's unnecessary, as you observed.
Edit: To demonstrate that return exits immediately, consider this function:
def last_name(name)
return nil unless name
name.split(/\s+/)[-1]
end
If you call this function as last_name("Antal S-Z"), it will return "S-Z". If you call it as last_name(nil), it returns nil. If return didn't abort immediately, it would try to execute nil.split(/\s+/)[-1], which would throw an error.
Using "return" is unnecessary if it is the last line to be executed in the method, since Ruby automatically returns the last evaluated expression.
You don't even need that final "mood", nor do you need those assignments in the IF statement.
def write_code(number_of_errors)
if number_of_errors > 1
"ERROR"
else
"No Problem"
end
end
puts write_code(10)
Output:
ERROR
I use return when I'm going through a list, and I want to exit the function if any member of the list meets a criteria. I could accomplish this with a single statement like:
list.select{|k| k.meets_criteria}.length == 0
in some situations, but
list.each{|k| return false if k.meets_criteria}
is one line too--with, to my mind, some added flexibility. For example, the first example assumes that this is the only line in the method, and that we want to return from this point no matter what. But if this is a test to see whether it is safe to proceed with the rest of the method, the first example will need to handle that in a different way.
EDIT:
To add some flexibility, consider the following line of code:
list_of_method_names_as_symbols.each{|k| list_of_objects.each{|j| return k if j.send(k)}}
I'm sure this can be accomplished, in one line, without return, but off the top of my head I don't see how.
But this is now a fairly flexible line of code that can be called with any list of boolean methods and a list of objects that implement those methods.
EDIT
It should be noted that I'm assuming this line is inside a method, not a block.
But this is mostly a stylistic choice, I think that in most situations, you can and possibly should avoid using return.
Ruby returns always! the best way is
def write_code(number_of_errors)
(number_of_errors > 1)? "ERROR" : "No Problem"
end
it means that if number_of_errors > 1 it will return ERROR else No Problem
Its nice ruby gives this good feature of not specifying return statement explicitly but I just feel, as a programming standard, one should always strive to specify "return" statements wherever required. This helps in making code more readable for someone who is coming from different background like C++, Java, PHP etc. and learning ruby. "return" statement will not harm anything, so why skip conventional and more standard way of returning from functions.
One small caution for those coming from other languages. Say you have a function like the OP's, and you make use of the "last thing computed" rule to set your return value automagically:
def write_code(number_of_errors)
if number_of_errors > 1
mood = "Ask me later"
else
mood = "No Problem"
end
end
and let's say you add a debugging (or logging) statement:
def write_code(number_of_errors)
if number_of_errors > 1
mood = "Ask me later"
else
mood = "No Problem"
end
puts "### mood = #{mood}"
end
Now guess what. You've broken your code, because the puts returns nil, which now becomes the return value from the function.
The solution is to get in the habit of always explicitly putting the return value on the last line, the way the OP did:
def write_code(number_of_errors)
if number_of_errors > 1
mood = "Ask me later"
else
mood = "No Problem"
end
puts "### mood = #{mood}"
mood
end
Unnecesarity of return at the last line in function is just syntaxic sugar of Ruby. In most procedural languages you need to write return in each (non-void in C++) function.

How to break outer cycle in Ruby?

In Perl, there is an ability to break an outer cycle like this:
AAA: for my $stuff (#otherstuff) {
for my $foo (#bar) {
last AAA if (somethingbad());
}
}
(syntax may be wrong), which uses a loop label to break the outer loop from inside the inner loop. Is there anything similar in Ruby?
Consider throw/catch. Normally the outside loop in the below code will run five times, but with throw you can change it to whatever you like, breaking it in the process. Consider this perfectly valid ruby code:
catch (:done) do
5.times { |i|
5.times { |j|
puts "#{i} #{j}"
throw :done if i + j > 5
}
}
end
What you want is non-local control-flow, which Ruby has several options for doing:
Continuations,
Exceptions, and
throw/catch
Continuations
Pros:
Continuations are the standard mechanism for non-local control-flow. In fact, you can build any non-local control-flow (subroutines, procedures, functions, methods, coroutines, state machines, generators, conditions, exceptions) on top of them: they are pretty much the nicer twin of GOTO.
Cons:
Continuations are not a mandatory part of the Ruby Language Specification, which means that some implementations (XRuby, JRuby, Ruby.NET, IronRuby) don't implement them. So, you can't rely on them.
Exceptions
Pros:
There is a paper that proves mathematically that Exceptions can be more powerful than Continuations. IOW: they can do everything that continuations can do, and more, so you can use them as a replacement for continuations.
Exceptions are universally available.
Cons:
They are called "exceptions" which makes people think that they are "only for exceptional circumstances". This means three things: somebody reading your code might not understand it, the implementation might not be optimized for it (and, yes, exceptions are godawful slow in almost any Ruby implementation) and worst of all, you will get sick of all those people constantly, mindlessly babbling "exceptions are only for exceptional circumstances", as soon as they glance at your code. (Of course, they won't even try to understand what you are doing.)
throw/catch
This is (roughly) what it would look like:
catch :aaa do
stuff.each do |otherstuff|
foo.each do |bar|
throw :aaa if somethingbad
end
end
end
Pros:
The same as exceptions.
In Ruby 1.9, using exceptions for control-flow is actually part of the language specification! Loops, enumerators, iterators and such all use a StopIteration exception for termination.
Cons:
The Ruby community hates them even more than using exceptions for control-flow.
No, there isn't.
Your options are:
put the loop in a method and use return to break from the outer loop
set or return a flag from the inner loop and then check that flag in the outer loop and break from it when the flag is set (which is kind of cumbersome)
use throw/catch to break out of the loop
while c1
while c2
do_break=true
end
next if do_break
end
or "break if do_break" depending on what you want
Perhaps this is what you want? (not tested)
stuff.find do |otherstuff|
foo.find do
somethingbad() && AAA
end
end
The find method keeps looping until the block returns a non null value or the end of the list is hit.
Wrapping an internal method around the loops could do the trick
Example:
test = [1,2,3]
test.each do |num|
def internalHelper
for i in 0..3
for j in 0..3
puts "this should happen only 3 times"
if true
return
end
end
end
end
internalHelper
end
Here you can do a check inside any of the for loops and return from the internal method once a condition is met.
I know I will regret this in the morning but simply using a while loop could do the trick.
x=0
until x==10
x+=1
y=0
until y==10
y+=1
if y==5 && x==3
x,y=10,10
end
end
break if x==10
puts x
end
The if y==5 && x==3 is only an example of an expression turning true.
You may consider adding a flag, which is set inside the inner loop, for controlling the outer loop.
'next' the outer loop
for i in (1 .. 5)
next_outer_loop = false
for j in (1 .. 5)
if j > i
next_outer_loop = true if j % 2 == 0
break
end
puts "i: #{i}, j: #{j}"
end
print "i: #{i} "
if next_outer_loop
puts "with 'next'"
next
end
puts "withOUT 'next'"
end
'break' the outer loop
for i in (1 .. 5)
break_outer_loop = false
for j in (1 .. 5)
if j > i
break_outer_loop = true if i > 3
break
end
puts "i: #{i}, j: #{j}"
end
break if break_outer_loop
puts "i: #{i}"
end

Resources