What does Ruby's BEGIN do? - ruby

What does BEGIN mean in Ruby, and how is it called? For example, given this code:
puts "This is sentence 1."
BEGIN {
puts "This is sentence 2."
}
why is puts "This is sentence 2." executed first?

BEGIN and END set up blocks that are called before anything else gets executed, or after everything else, just before the interpreter quits.
For instance, running this:
END { puts 'END block' }
puts 'foobar'
BEGIN { puts 'BEGIN block' }
Outputs:
BEGIN block
foobar
END block
Normally we'd use a bit more logical order for the BEGIN and END blocks, but that demonstrates what they do.

From the Ruby docs for the BEGIN keyword:
BEGIN: Designates, via code block, code to be executed unconditionally before sequential execution of the program begins. Sometimes used to simulate forward references to methods.

BEGIN and END Blocks
Every Ruby source file can declare blocks of code to be run as the file is being loaded (the BEGIN blocks) and after the program has finished executing (the END blocks).
BEGIN {
begin block code
}
END {
end block code
}
A program may include multiple BEGIN and END blocks. BEGIN blocks are executed in the order they are encountered. END blocks are executed in reverse order.
You can find almost the same post in "Does begin . . . end while denote a 'block'?".
Read more about blocks on tutorialspoint

Related

For loop only going through one iteration

I am trying to create a program that simplifies radicals in TI-BASIC. However, one FOR loop seems to be only completing one iteration while the rest work fine. Here is the code:
Input "Root=",A
Input "Radical=",B
B→Z
ClrList L₃
prgmPRMNTOL1
prgmGETPRIME
The FOR loop in question starts here...
For(Y,1,10)
0→Z
For(X,1,dim(L₂))
If L₁(Y)=L₂(X)
Then
Z+1→Z
End:Disp Z
End
If Z≥A
Then
int(Z/A)*A→C
int(Z/A)→D
For(T,1,D)
L₁(Y)→L₃(1+dim(L₃))
End
For(R,1,C)
ClrList L₄
For(S,1,dim(L₂))
If L₂(S)=L₁(Y) and C>0
Then
–1→L₂(S)
C-1→C
End
End
For(Q,1,dim(L₂))
If L₂(Q)≠–1
Then
L₂(Q)→L₄(1+dim(L₄))
End
End
ClrList L₂
For(Q,1,dim(L₄))
L₄(Q)→L₂(Q)
End
End
End
...and ends here.
1→E
For(M,1,dim(L₃))
E*L₃(M)→E
End
1→F
For(N,1,dim(L₂))
F*L₂(N)→F
End
Disp "OUTSIDE",E,"ROOT",A,"INSIDE",F
The program works perfectly besides the fact that this one loop only runs once. I will post prgmPRMNTOL1 and prgmGETPRIME as well if necessary, though they probably do not have any impact as they modify only L₁ and L₂.
EDIT: Added a indented version.
Input "Root=",A
Input "Radical=",B
B→Z
ClrList L₃
prgmPRMNTOL1
prgmGETPRIME
For(Y,1,10)
0→Z
For(X,1,dim(L₂))
If L₁(Y)=L₂(X)
Then
Z+1→Z
End
End
If Z≥A
Then
int(Z/A)*A→C
int(Z/A)→D
For(T,1,D)
L₁(Y)→L₃(1+dim(L₃))
End
For(R,1,C)
ClrList L₄
For(S,1,dim(L₂))
If L₂(S)=L₁(Y)
Then
–1→L₂(S)
End
End
For(Q,1,dim(L₂))
If L₂(Q)≠–1
Then
L₂(Q)→L₄(1+dim(L₄))
End
End
ClrList L₂
For(Q,1,dim(L₄))
L₄(Q)→L₂(Q)
End
End
End
1→E
For(M,1,dim(L₃))
E*L₃(M)→E
End
1→F
For(N,1,dim(L₂))
F*L₂(N)→F
End
Disp "OUTSIDE",E,"ROOT",A,"INSIDE",F
Close Your Loops
Your issue seems to stem from incorrectly matching your If statements, loops and their End statements.
What's Happening
The behavior of TI-Basic when end of file is reached before all loops and If statements have been closed is to fail silently, terminating the program, rather than alerting the users of an error.
This behavior makes certain odd pieces of code syntactically valid. For instances this snippet would run without error:
For(A,1,10
Disp A
The results being
1
Done
Obviously this is the same as running Disp 1 so, there's no reason to use this an your code. It serves only to make silent and annoying errors appear in code.
The Fix
Assuming the indented version of your code represents how you want your loops to be nested, the code below should fix your problem.
Input "Root=",A
Input "Radical=",B
B→Z
ClrList L₃
prgmPRMNTOL1
prgmGETPRIME
For(Y,1,10)
0→Z
For(X,1,dim(L₂))
If L₁(Y)=L₂(X)
Z+1→Z
End
If Z≥A:Then
int(Z/A)*A→C
int(Z/A)→D
For(T,1,D)
L₁(Y)→L₃(1+dim(L₃))
End
For(R,1,C)
ClrList L₄
For(S,1,dim(L₂))
If L₂(S)=L₁(Y)
–1→L₂(S)
End
End:"Inserted this End
For(Q,1,dim(L₂))
If L₂(Q)≠–1
L₁(Q)→L₄(1+dim(L₄))
End
ClrList L₂
For(Q,1,dim(L₄))
L₄(Q)→L₂(Q)
End
End
End
1→E
For(M,1,dim(L₃))
E*L₃(M)→E
End
1→F
For(N,1,dim(L₂))
F*L₂(N)→F
End
Disp "OUTSIDE",E,"ROOT",A,"INSIDE",F
If that doesn't fix it, the missing End is elsewhere in your code.

how to execute multiple statements within a ruby code block

I have the following simplified code example:
class MyTimerClass
def timed_execution(&block)
...
end
def elapsed_time
...
return ...
end
end
...
t = MyTimerClass.new
t.timed_execution {1000.times {"foo".equal? "foo"}}
puts "block took #{t.elapsed_time} seconds to run."
What I want to do is print out "executed" on every execution of "foo".equal? "foo". Instead of putting this inside the timed_execution method I want to add it to the passed code block. something like...
t.timed_execution {1000.times {"foo".equal? "foo" puts "executed"}}
which is wrong. I guess what I really want to know is how to have multiple statements within a code block. Really simple question I know...
You can use do .. end instead of braces. Or you can even separate the statements with a semicolon.
t.timed_execution do
1000.times do
"foo".equal? "foo"
puts "executed"
end
end
OR
t.timed_execution { 1000.times { "foo".equal? "foo" ; puts "executed" } }
From the question, it seems that you had the idea that a code block in Ruby should be written on a single line. In fact, code blocks in Ruby can span over multiple lines.
Code blocks in Ruby are created by writing statements inside an opening '{' and an ending '}' or an opening 'do' and an ending 'end'. Arguments to the block go on the same line as the block opening. Example :
# Using 'do' and 'end'.
5.times do |i|
puts i
end
# Using '{' and '}'.
5.times { |i|
puts i
}
The above way of writing code blocks are preferred if you are going to have more than one statement inside the block. If you are sure you are only gonna need a single statement then you can also opt to follow one line block style (many people do I think, I myself personally go for this one for a single statement). Example :
# Using a single line.
5.times { |i| puts i }
It's allowed to put multiple statements inside a single line block with the help of semi-colons to separate statements, also it's okay to use multi-line block for a single statement. But its a good idea to not do the former one for better readability.
Also from my opinion it's better to use '{' and '}' for single line blocks and 'do' and 'end' for multi-line blocks.

ruby rescue block -- respond with more than just one command

I'm running a script with an API that often times out. I'm using begin/rescue blocks to get it to redo when this happens, but want to log what is happening to the command line before I run the redo command.
begin
#...api query...
rescue ErrorClass
puts("retrying #{id}") && redo
end
Unfortunately the above script doesn't work. Only the first command is run.
I would like to force the rescue block to run multiple lines of code like so:
begin
# api query
rescue ErrorClass do ###or:# rescue ErrorClass do |e|
puts "retrying #{id}"
redo
end
but those don't work either.
I've had luck creating a separate method to run like so:
def example
id = 34314
begin
5/0
rescue ZeroDivisionError
eval(handle_zerodiv_error(id))
end
end
def handle_zerodiv_error(id)
puts "retrying #{id}"
"redo"
end
...that actually works. But it requires too many lines of code in my opinion and it uses eval which is not kosher by any means according to my mentor(s).
You are unnecessarily complicating things by using && or do. The && version does not work because puts returns nil, so by shortcut evaluation of &&, the part to follow is not evaluated. If you use || or ; instead, then it will work:
begin
...
rescue ErrorClass
puts("retrying #{id}") || redo
end
begin
...
rescue ErrorClass
puts("retrying #{id}"); redo
end
but even this is not necessary. You somehow seem to believe that you need a block within rescue to write multiple lines, but that does not make sense because you are not using a block with single line. There is no Ruby construction that requires a block only when you have multiple lines. So, just put them in multiple lines:
begin
...
rescue ErrorClass
puts("retrying #{id}")
redo
end
There is a retry built in. This example is from "The Ruby Programming Language" pg 162.
require "open-uri"
tries = 0
begin
tries +=1
open("http://www.example.com/"){|f| puts f.readlines}
rescue OpenURI::HTTPError => e
puts e.message
if (tries < 4)
sleep (2**tries) # wait for 2, 4 or 8 seconds
retry # and try again
end
end

How could one block detect that its inside another block?

This is my code:
def block
puts "from block"
yield
end
block do
puts "from command line"
block do
end
end
Here is the output:
from block
from command line
from block
I wonder how the second block could detect that its inside another block (of the same method).
So that the output will be this instead:
from block 1
from command line
from block 2
Is this possible? Because I want the nested block to be aware of this and run some additional code.
Thanks!
You could keep track of the block level with an instance variable, increment it whenever you enter a block, and decrement it whenever you leave a block:
def block
#block_level ||= 0
#block_level += 1
puts "from block ##block_level"
yield
#block_level -= 1
end
This answer is mostly just for fun, I don't suggest you use it.
Ruby lets you inspect the call stack in the form of a backtrace, but only when an exception is raised. So let's raise an exception and then stick out our arm and catch it before it goes to anyone else, and then: the backtrace is all ours!!
Then all you need to do is search the backtrace (an array) for any method calls to our method named "block", and count them.
class InspectBacktrace < Exception
end
def block
raise InspectBacktrace
rescue InspectBacktrace => e
level = e.backtrace.count { |x| x =~ /in `block'/ }
puts "from block #{level}"
yield
end
block do
puts "from command line"
block do
puts "from command line"
block do
puts "from command line"
end
end
end
Output:
from block 1
from command line
from block 2
from command line
from block 3
from command line
Edit: I've since come across the Kernel#caller method which just gives you the current execution stack with no hassles. So the following code might be acceptable, as long as you don't have two methods named "block" in the same file that call each other:
def block
level = caller.count { |x| x =~ /^#{ Regexp.escape(__FILE__) }:\d+:in `block'$/ } + 1
puts "from block #{level}"
yield
end
What yjerem says, just use ensure to avoid troubles with exceptions, and it sounds like a global variable, not instance variable.
def block
begin
$block_level ||= 0
$block_level += 1
puts "from block #{$block_level}"
yield
ensure
$block_level -= 1
end
end

Does begin . . . end while denote a 'block'?

temp = 98.3
begin
print "Your temperature is " + temp.to_s + " Fahrenheit. "
puts "I think you're okay."
temp += 0.1
end while temp < 98.6
In the above example, is everything between begin and end a block?
I'm still confused what a block is.
If you can't call it a block, what would you call that chunk of code between begin and end? Is it ok to call it a chunk?
Block has a special meaning in Ruby. According to Matz, Ruby's creator, you can look at a block as a nameless function - typically something that can be yielded into, and which may also take parameters.
You may see the following kind of disamiguation when describing Ruby syntax:
begin...end (what is called block in other languages) may sometimes be referred to simply as what it is, i.e. an expression (which may in turn contain other expressions - an expression is simply something that has a return value) in Ruby. Some references will still call it a begin/end block, or a code block, adding somewhat to the confusion
do...end or {...} will always be referred to as a block in Ruby
For examples, peruse the the Ruby syntax man page, e.g.
begin expression end
expression while expression
loop block
For further reading, see:
Programming Ruby
Ruby (from other languages)
Much, much more documentation
begin/end are strictly control flow, not blocks.
begin
puts "hi"
end
# => "hi"
The code runs immediately. If it was a block, it would have to been called somehow in order for the code in it to run, as in this example:
def a_method; end
a_method { puts "hi" }
# nothing..
def a_method
yield
end
a_method { puts "Hi!" }
# => "Hi!"

Resources