Module: Kernel
- Defined in:
- lib/rubyexts.rb,
lib/rubyexts/kernel.rb
Instance Method Summary collapse
-
#acquire(glob, params = Hash.new) ⇒ Array<String>
Require all files matching given glob relative to $:.
- #acquire!(glob, params = Hash.new) ⇒ Object
-
#acquire_relative(glob, params = Hash.new) ⇒ Array<String>
Require all files matching given glob relative to current file.
- #acquire_relative!(glob, params = Hash.new) ⇒ Object
- #command(command) ⇒ Object (also: #sh, #run)
- #load_relative(file) ⇒ Object
- #metaclass ⇒ Object
-
#os ⇒ Object
os.home.
- #p_and_return(*args) ⇒ Object
-
#puts_and_return(*args) ⇒ Object
for quick inspection.
- #quiet(&block) ⇒ Object
- #quiet!(&block) ⇒ Object
- #require_gem_or_exit(library, gemname = library, options = Hash.new) ⇒ Object
- #try_require(library) ⇒ Object
-
#try_require_gem(library, gemname = library, options = Hash.new) ⇒ Boolean
True if require was successful, false otherwise.
- #try_require_gem!(library, gemname = library, options = Hash.new) ⇒ Object
Instance Method Details
#acquire(glob, params = Hash.new) ⇒ Array<String>
Require all files matching given glob relative to $:
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/rubyexts.rb', line 22 def acquire(glob, params = Hash.new) base, glob = get_base_and_glob(glob) $:.compact.find do |path| fullpath = File.(File.join(path, base)) if File.directory?(fullpath) return __acquire__(fullpath, glob, params.merge(soft: true)) end end raise LoadError, "Directory #{base} doesn't exist in $:" end |
#acquire!(glob, params = Hash.new) ⇒ Object
33 34 35 |
# File 'lib/rubyexts.rb', line 33 def acquire!(glob, params = Hash.new) self.acquire(glob, params.merge(soft: false)) end |
#acquire_relative(glob, params = Hash.new) ⇒ Array<String>
Require all files matching given glob relative to current file
51 52 53 54 55 56 57 |
# File 'lib/rubyexts.rb', line 51 def acquire_relative(glob, params = Hash.new) base, glob = get_base_and_glob(glob) path = File.dirname(caller[0].split(":").first) full = File.(File.join(path, base)) raise LoadError, "Directory #{base} doesn't exist in #{path}" unless File.directory?(full) return __acquire__(full, glob, params.merge(soft: true)) end |
#acquire_relative!(glob, params = Hash.new) ⇒ Object
59 60 61 |
# File 'lib/rubyexts.rb', line 59 def acquire_relative!(glob, params = Hash.new) self.acquire_relative(glob, params.merge(soft: false)) end |
#command(command) ⇒ Object Also known as: sh, run
54 55 56 57 |
# File 'lib/rubyexts/kernel.rb', line 54 def command(command) puts command puts %x[#{command}] end |
#load_relative(file) ⇒ Object
63 64 65 66 |
# File 'lib/rubyexts.rb', line 63 def load_relative(file) path = File.dirname(caller[0].split(":").first) load File.(File.join(path, file)) end |
#metaclass ⇒ Object
5 6 7 8 9 |
# File 'lib/rubyexts/kernel.rb', line 5 def class << self self end end |
#p_and_return(*args) ⇒ Object
85 86 87 88 |
# File 'lib/rubyexts/kernel.rb', line 85 def p_and_return(*args) p(*args) return *args end |
#puts_and_return(*args) ⇒ Object
for quick inspection
79 80 81 82 |
# File 'lib/rubyexts/kernel.rb', line 79 def puts_and_return(*args) puts(*args) return *args end |
#quiet(&block) ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/rubyexts/kernel.rb', line 61 def quiet(&block) old_stdout = STDOUT.dup STDOUT.reopen("/dev/null") returned = block.call STDOUT.reopen(old_stdout) return returned end |
#quiet!(&block) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/rubyexts/kernel.rb', line 69 def quiet!(&block) old_stderr = STDERR.dup STDERR.reopen("/dev/null", "a") returned = quiet(&block) STDERR.reopen(old_stderr) return returned end |
#require_gem_or_exit(library, gemname = library, options = Hash.new) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/rubyexts/kernel.rb', line 40 def require_gem_or_exit(library, gemname = library, = Hash.new) gemname, = library, gemname if gemname.is_a?(Hash) && .empty? try_require_gem!(library, gemname, ) rescue LoadError exit 1 end |
#try_require(library) ⇒ Object
48 49 50 51 52 |
# File 'lib/rubyexts/kernel.rb', line 48 def try_require(library) require library rescue LoadError return false end |
#try_require_gem(library, gemname = library, options = Hash.new) ⇒ Boolean
Returns True if require was successful, false otherwise.
22 23 24 25 26 27 |
# File 'lib/rubyexts/kernel.rb', line 22 def try_require_gem(library, gemname = library, = Hash.new) gemname, = library, gemname if gemname.is_a?(Hash) && .empty? try_require_gem!(library, gemname, ) rescue LoadError return false end |
#try_require_gem!(library, gemname = library, options = Hash.new) ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/rubyexts/kernel.rb', line 30 def try_require_gem!(library, gemname = library, = Hash.new) gemname, = library, gemname if gemname.is_a?(Hash) && .empty? require library rescue LoadError => exception = "Gem #{gemname} isn't installed. Run sudo gem install #{gemname}. (#{exception.inspect})" puts raise exception end |