Module: Kernel
- Defined in:
- lib/nano/kernel/returning.rb,
lib/nano/kernel/p.rb,
lib/nano/kernel/fn.rb,
lib/nano/kernel/demo.rb,
lib/nano/kernel/here.rb,
lib/nano/kernel/nack.rb,
lib/nano/kernel/resc.rb,
lib/nano/kernel/maybe.rb,
lib/nano/kernel/bug%21.rb,
lib/nano/kernel/called.rb,
lib/nano/kernel/silently.rb,
lib/nano/kernel/get_by_id.rb,
lib/nano/kernel/autoreload.rb,
lib/nano/kernel/call_stack.rb,
lib/nano/kernel/__require__.rb,
lib/nano/kernel/method_name.rb,
lib/nano/kernel/require_all.rb,
lib/nano/kernel/require_esc.rb,
lib/nano/kernel/require_nano.rb,
lib/nano/kernel/require_local.rb,
lib/nano/kernel/warn_with_line.rb,
lib/nano/kernel/autoreload_glob.rb,
lib/nano/kernel/silence_warnings.rb
Overview
– Copied from Wee 0.8.0 © 2004 Michael Neumann. ++
Defined Under Namespace
Modules: NanoKernel
Class Method Summary collapse
-
.demo(out = $stdout, &block) ⇒ Object
For debugging and showing examples.
- .get_by_id(id) ⇒ Object
Instance Method Summary collapse
-
#autoreload(check_interval = 10) ⇒ Object
– TODO ++.
-
#autoreload_glob(glob, check_interval = 1) ⇒ Object
Note that this method will load any file that matches the glob and is modified since the method call, regardless whether it’s loaded by the current application or not.
-
#bug!(message = 'must not happen') ⇒ Object
Raises a ScritBug error with a message.
- #call_stack(level = 1) ⇒ Object
-
#maybe(chance = 0.5, &block) ⇒ Object
Random generator that returns true or false.
- #nack ⇒ Object
-
#p(x) ⇒ Object
alias_method :pr, :p REdefines standrard #p kernel method to pass through its argument.
-
#resc(x) ⇒ Object
Provides a shortcut to the Regexp.escape module method.
-
#returning(value) ⇒ Object
A Ruby-ized realization of the K combinator.
-
#silently ⇒ Object
(also: #silence_warnings)
Temporarily turn-off verbose mode while yielding block.
-
#warn_with_line(msg = "", fulltrace = nil) ⇒ Object
Like #warn produces the current line number as well.
Class Method Details
.demo(out = $stdout, &block) ⇒ Object
For debugging and showing examples. Currently this takes an argument of a string in a block.
demo {%{ a = [1,2,3] }}
demo {%{ a.slice(1,2) }}
demo {%{ a.map { |x| x**3 } }}
Produces:
a = [1,2,3] #=> [1, 2, 3]
a.slice(1,2) #=> [2, 3]
a.map { |x| x**3 } #=> [1, 8, 27]
– Is there a way to do this without the eval string in block? Preferably just a block and no string. ++
20 21 22 |
# File 'lib/nano/kernel/demo.rb', line 20 def demo(out=$stdout,&block) out << sprintf("%-25s#=> %s\n", expr = block.call, eval(expr, block.binding).inspect) end |
.get_by_id(id) ⇒ Object
3 4 5 |
# File 'lib/nano/kernel/get_by_id.rb', line 3 def get_by_id( id ) ObjectSpace._id2ref( id ) end |
Instance Method Details
#autoreload(check_interval = 10) ⇒ Object
– TODO ++
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/nano/kernel/autoreload.rb', line 11 def autoreload(check_interval=10) Thread.new(Time.now) { |start_time| file_mtime = {} loop { sleep check_interval $LOADED_FEATURES.each { |feature| $LOAD_PATH.each { |lp| file = File.join(lp, feature) if (File.exists?(file) and File.stat(file).mtime > (file_mtime[file] || start_time)) file_mtime[file] = File.stat(file).mtime STDERR.puts "reload #{ file }" begin load(file) rescue Exception => e STDERR.puts e.inspect end end } } } } end |
#autoreload_glob(glob, check_interval = 1) ⇒ Object
Note that this method will load any file that matches the glob and is modified since the method call, regardless whether it’s loaded by the current application or not. The glob is expanded only once at the initial method call.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/nano/kernel/autoreload_glob.rb', line 13 def autoreload_glob(glob, check_interval=1) files = Dir.glob(glob) file_mtime = {} start_time = Time.now Thread.new { loop { sleep check_interval files.each { |file| if (File.exists?(file) and File.stat(file).mtime > (file_mtime[file] || start_time)) file_mtime[file] = File.stat(file).mtime STDERR.puts "reload #{ file }" begin load(file) rescue Exception => e STDERR.puts e.inspect end end } } } end |
#bug!(message = 'must not happen') ⇒ Object
Raises a ScritBug error with a message.
if find_bug then
bug! "unknown bug found"
end
– Credit goes to Minero Aoki. Cute! :-) ++
14 15 16 |
# File 'lib/nano/kernel/bug%21.rb', line 14 def bug!( = 'must not happen' ) raise ScriptBug, "\n[SCRIPT BUG] " + end |
#call_stack(level = 1) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/nano/kernel/call_stack.rb', line 23 def call_stack( level = 1 ) call_str_array = pp_call_stack(level) stack = [] call_str_array.each{ |call_str| file, lineno, method = call_str.split(':') if method =~ /in `(.*)'/ then method = $1.intern() end stack << [file, lineno.to_i, method] } stack end |
#maybe(chance = 0.5, &block) ⇒ Object
Random generator that returns true or false. Can also take a block that has a 50/50 chance to being executed.
maybe #=> true
maybe #=> false
8 9 10 11 12 13 14 |
# File 'lib/nano/kernel/maybe.rb', line 8 def maybe(chance = 0.5, &block) if block then yield if rand < chance else rand < chance end end |
#p(x) ⇒ Object
alias_method :pr, :p REdefines standrard #p kernel method to pass through its argument.
5 6 7 8 |
# File 'lib/nano/kernel/p.rb', line 5 def p( x ) puts x.inpsect x end |
#resc(x) ⇒ Object
Provides a shortcut to the Regexp.escape module method.
3 4 5 |
# File 'lib/nano/kernel/resc.rb', line 3 def resc(x) Regexp.escape(x) end |
#returning(value) ⇒ Object
A Ruby-ized realization of the K combinator.
def foo
returning values = [] do
values << 'bar'
values << 'baz'
end
end
foo # => ['bar', 'baz']
17 18 19 20 |
# File 'lib/nano/kernel/returning.rb', line 17 def returning(value) yield value end |
#silently ⇒ Object Also known as: silence_warnings
Temporarily turn-off verbose mode while yielding block.
4 5 6 7 8 9 10 11 |
# File 'lib/nano/kernel/silently.rb', line 4 def silently #:yield: old_verbose, $VERBOSE = $VERBOSE, nil begin yield ensure $VERBOSE = old_verbose end end |
#warn_with_line(msg = "", fulltrace = nil) ⇒ Object
Like #warn produces the current line number as well.
warn_with_line("You have been warned.")
produces
3: Warning: You have been warned.
Note that this method depends on the output of #caller.
11 12 13 14 15 16 |
# File 'lib/nano/kernel/warn_with_line.rb', line 11 def warn_with_line(msg="", fulltrace=nil) trace = caller(1) where = trace[0].sub(/:in.*/,'') STDERR.puts "#{where}: Warning: #{msg}" STDERR.puts trace.map { |t| "\tfrom #{t}" } if fulltrace end |