Skip to content

Commit

Permalink
fix bad format
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengjia committed Apr 24, 2011
1 parent ab1f746 commit 683514b
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion app/tutorial_1/tutorial_1.md
Expand Up @@ -130,7 +130,7 @@ The last form of set method accepts a hash and split the hash to set individual

Finally the set method returns self, which is Sinatra::Base so other methods can be chained to set method. However I've never seen any cases this can be useful.

Then we come to the `caller_files` and it's associated code. caller_files is a public class method of Sinatra::Base. `CALLERS_TO_IGNORE` is a constant that defines the patterns that should be ignored from result of the `Kernel#caller`. The first regular expression is kind of special. It matches `/sinatra.rb`, `/sinatra/base.rb`, `/sinatra/main.rb`, and `/sinatra/showexceptions.rb`. `RUBY_IGNORE_CALLERS` is added to CALLERS_TO_IGNORE if it's available. caller_locations calls the Kernel#caller method, which basically returns the calling stack in the format like `/Users/zjia/code/ruby_test/caller/caller.rb:3:in `<main>'`. The `caller(1)` will ignore the top level of the calling stack, i.e., the `sinatra/lib/sinatra/main.rb` itself. Regex `/:(?=\d|in )/` matches a colon preceding a number or a string 'in', but not including the number or 'in'. For example in `/Users/zjia/code/ruby_test/caller/caller.rb:3:in `<main>'` it will match the two colons. Then `/Users/zjia/code/ruby_test/caller/caller.rb:3:in `<main>'` is splitted at the two colons and [0,2] get the first two elements of the array returned by the split, i.e., the pure file location and the line number. Finally the reject method uses the patterns in CALLERS_TO_IGNORE to remove the unwanted lines of the calling stack. The `caller_files` further removes the line number and returns only the pure file location.
Then we come to the `caller_files` and it's associated code. caller_files is a public class method of Sinatra::Base. `CALLERS_TO_IGNORE` is a constant that defines the patterns that should be ignored from result of the `Kernel#caller`. The first regular expression is kind of special. It matches `/sinatra.rb`, `/sinatra/base.rb`, `/sinatra/main.rb`, and `/sinatra/showexceptions.rb`. `RUBY_IGNORE_CALLERS` is added to CALLERS_TO_IGNORE if it's available. caller_locations calls the Kernel#caller method, which basically returns the calling stack in the format like /Users/zjia/code/ruby_test/caller/caller.rb:3:in `<main>'. The `caller(1)` will ignore the top level of the calling stack, i.e., the `sinatra/lib/sinatra/main.rb` itself. Regex `/:(?=\d|in )/` matches a colon preceding a number or a string 'in', but not including the number or 'in'. For example in /Users/zjia/code/ruby_test/caller/caller.rb:3:in `<main>' it will match the two colons. Then /Users/zjia/code/ruby_test/caller/caller.rb:3:in `<main>' is splitted at the two colons and [0,2] get the first two elements of the array returned by the split, i.e., the pure file location and the line number. Finally the reject method uses the patterns in CALLERS_TO_IGNORE to remove the unwanted lines of the calling stack. The `caller_files` further removes the line number and returns only the pure file location.

We return to the line `set :app_file, caller_files.first || $0`. As the source annotation says, `caller_files.first` is the file that calls `require 'sinatra'`. As we talked, when `require 'sinatra'` is called, it requires sinatra/lib/sinatra.rb, which requires sinatra/lib/sinatra/main.rb. sinatra/lib/sinatra.rb and /sinatra/lib/sinatra/main.rb are in the ignored patterns so they are removed from caller_files. Then the first element in the array should be the one that contains the `requires 'sinatra'`. Here I think `caller(1)` in caller_locations is not necessary because the top level of the calling stack sinatra/lib/sinatra/main.rb is in the ignored pattern. If caller_files is an empty array, which is possible when the file is located in the ignored paths, then the current running file stored in $0 is set as the the `app_file`. `app_file` stores the root path of the sinatra project and locations of other files are based on it.

Expand Down

0 comments on commit 683514b

Please sign in to comment.