How do I get ruby to print a full backtrace instead of a truncated one?
Exception#backtrace has the entire stack in it:
def do_division_by_zero; 5 / 0; end
begin
do_division_by_zero
rescue => exception
puts exception.backtrace
raise # always reraise
end
(Inspired by Peter Cooper's Ruby Inside blog)
How can I force Ruby to show a full stack trace?
begin
# Code that raises exception
rescue StandardError => e
puts e.backtrace
end
Log exact exception origin and its backtrace at_exit in Ruby
Backtrace already contains file and lineno, $@
global contains a backtrace.
How do I get this block of ruby to add each individual hash into an array instead of just adding one hash multiple times?
Create sessions
inside of the each_with_index
block instead of outside:
sessions_array = []
@session.each do |element|
sessions = {
time: element[0],
action: element[1],
user: element[2],
}
sessions_array << sessions
end
puts sessions_array
However, this can be done much more succinctly. When you're turning an array into another array with the same number of elements you almost always want to use map
. Also, in a Ruby block you can extract the elements from an array by specifying multiple names in its arguments (|foo, bar, ...|
).
This code is equivalent to the above:
sessions_array = @session.map do |time, action, user|
{ time: time, action: action, user: user }
end
You can see both of these snippets in action on repl.it here: https://repl.it/@jrunning/NavyImmaculateShockwave
Related Topics
Jekyll Serve Dependency Error - Could Not Open 'Lib Curl'
In Ruby Why Does Nil.Id Return 4
In Ruby, Can You Perform String Interpolation on Data Read from a File
Ruby Mocha: Is There an Equivalent to Rspec-Mocks' #And_Call_Original
Ruby: Intersection Between Two Ranges
Ruby Gem Dependencies on Offline Server
How to Use Rspec to Mock Stdin/Stdout to Test Console Reads & Writes
Using Rbenv Doesn't Work with Sudo
Ruby Modulo 3 with Negative Numbers Is Unintuitive
Assign/Replace Params Hash in Rails
Ruby Ssl Error - Sslv3 Alert Unexpected Message
Functional Code Examples in Ruby
How to Overwrite a Printed Line in the Shell with Ruby