Module: Kernel

Defined in:
lib/rubyexts.rb,
lib/rubyexts/kernel.rb

Instance Method Summary collapse

Instance Method Details

#acquire(glob, params = Hash.new) ⇒ Array<String>

Require all files matching given glob relative to $:

Examples:

acquire "lib/*"
acquire "lib/**/*", exclude: "**/*_spec.rb"
acquire "lib/**/*", exclude: ["**/*_spec.rb", "lib/init.rb"]

Parameters:

  • library (String)

    Glob of files to require

  • params (Hash) (defaults to: Hash.new)

    Optional parameters.

Options Hash (params):

  • :exclude (String, Array<String>)

    File or list of files or globs relative to base directory

Returns:

  • (Array<String>)

    List of successfully loaded files

Raises:

  • (LoadError)

    If base directory doesn’t exist

  • (ArgumentError)

    If first argument isn’t a glob

Author:

  • Botanicus

Since:

  • 0.0.3



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.expand_path(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

Examples:

acquire "lib/*"
acquire "lib/**/*", exclude: "**/*_spec.rb"
acquire "lib/**/*", exclude: ["**/*_spec.rb", "lib/init.rb"]

Parameters:

  • library (String)

    Glob of files to require

  • params (Hash) (defaults to: Hash.new)

    Optional parameters.

Options Hash (params):

  • :exclude (String, Array<String>)

    File or list of files or globs relative to base directory

Returns:

  • (Array<String>)

    List of successfully loaded files

Raises:

  • (LoadError)

    If base directory doesn’t exist

  • (ArgumentError)

    If first argument isn’t a glob

Author:

  • Botanicus

Since:

  • 0.0.3



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.expand_path(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.expand_path(File.join(path, file))
end

#metaclassObject

Since:

  • 0.0.2



5
6
7
8
9
# File 'lib/rubyexts/kernel.rb', line 5

def metaclass
  class << self
    self
  end
end

#osObject

os.home



12
13
14
# File 'lib/rubyexts/kernel.rb', line 12

def os
  @os ||= OS.parse
end

#p_and_return(*args) ⇒ Object

Since:

  • 0.0.2



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

Since:

  • 0.0.2



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

Since:

  • 0.0.3



40
41
42
43
44
45
# File 'lib/rubyexts/kernel.rb', line 40

def require_gem_or_exit(library, gemname = library, options = Hash.new)
  gemname, options = library, gemname if gemname.is_a?(Hash) && options.empty?
  try_require_gem!(library, gemname, options)
rescue LoadError
  exit 1
end

#try_require(library) ⇒ Object

Since:

  • 0.0.3



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.

Examples:

try_require_gem "term/ansicolor", "term-ansicolor"

Parameters:

  • library (String)

    Library to require.

  • gemname (String, @optional) (defaults to: library)

    Name of gem which contains required library. Will be used for displaying message.

Returns:

  • (Boolean)

    True if require was successful, false otherwise.

Since:

  • 0.0.1



22
23
24
25
26
27
# File 'lib/rubyexts/kernel.rb', line 22

def try_require_gem(library, gemname = library, options = Hash.new)
  gemname, options = library, gemname if gemname.is_a?(Hash) && options.empty?
  try_require_gem!(library, gemname, options)
rescue LoadError
  return false
end

#try_require_gem!(library, gemname = library, options = Hash.new) ⇒ Object

Since:

  • 0.0.3



30
31
32
33
34
35
36
37
# File 'lib/rubyexts/kernel.rb', line 30

def try_require_gem!(library, gemname = library, options = Hash.new)
  gemname, options = library, gemname if gemname.is_a?(Hash) && options.empty?
  require library
rescue LoadError => exception
  message  = "Gem #{gemname} isn't installed. Run sudo gem install #{gemname}. (#{exception.inspect})"
  puts message
  raise exception
end