Module: WebTools::Support::Ruby

Defined in:
lib/web_tools/support/ruby.rb

Overview

Provide information about the Ruby environment

Constant Summary collapse

MODULE_MOD_FNS =
Module.instance_methods(false)

Class Method Summary collapse

Class Method Details

.const_info_for(class_name, const_name) ⇒ String

Return a display string suitable for rendering a constant. Currently, it just returns the results of #inspect on the object.

TODO

1. If the const value is a proc, then return "Proc: " + the source code.

Parameters:

  • class_name (String, Symbol)

    The name of the class to query.

  • const_name (String, Symbol)

    The name of the constant.

Returns:

  • (String)

    A description of the constant.



55
56
57
58
59
# File 'lib/web_tools/support/ruby.rb', line 55

def const_info_for(class_name, const_name)
  klass = find_in_namespace class_name
  const = klass.const_get const_name
  const.inspect
end

.module_fns_for(mod) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/web_tools/support/ruby.rb', line 15

def module_fns_for(mod)
  res = []
  begin
    sclass = mod.__singleton_class
    res = sclass.instance_methods(false) - MODULE_MOD_FNS
  rescue => e
    # nothing
  end
  res
end

.module_info_for(name) ⇒ Object



7
8
9
10
11
12
# File 'lib/web_tools/support/ruby.rb', line 7

def module_info_for(name)
  mod = find_in_namespace name
  { :constants        => mod.constants.sort,
    :class_methods    => module_fns_for(mod),
    :instance_methods => mod.instance_methods(false).sort }
end

.source_for(class_name, method_name, instance_method = true) ⇒ Object

Get the source code for the named class and method

The success of this method depends on being called from a Ruby stack, not a Smalltalk stack.

Parameters:

  • The (String)

    name of the class

  • The (String)

    method name

  • if (Boolean)

    true, then look for a class method, otherwise look for an instance method.



36
37
38
39
40
41
42
# File 'lib/web_tools/support/ruby.rb', line 36

def source_for(class_name, method_name, instance_method=true)
  klass = find_in_namespace(class_name)
  gsnmeth = klass.gs_method_for(method_name, instance_method)
  src = gsnmeth.__source_string
  file,line = gsnmeth.__source_location
  file.nil? ? "#{src}\n# No file information available." : "#{src}\n##{file}:#{line}"
end