class Object

Public Instance Methods

declare_and_define_ivar(name:, value:, convenience: true) click to toggle source

Declare and define an instance variable for a given object. Name is the instance variable name and value is the item assigned to that instance variable. Convenience determines whether getter/setter methods are defined as well.

# File lib/aastdlib/object.rb, line 7
def declare_and_define_ivar(name:, value:, convenience: true)

    if name =~ /^@/

            iv_handle = name
            name = name.to_s.gsub('@','')

    else

            iv_handle = ('@'+name)

    end

    iv_handle = iv_handle.to_sym if iv_handle.respond_to?(:to_sym)

    self.instance_variable_set(iv_handle, value)

    if convenience

            # getter
            self.define_singleton_method(name) do
                    self.instance_variable_get(iv_handle)
            end

            # setter
            self.define_singleton_method(name+'=') do |value|
                    self.instance_variable_set(iv_handle, value)
            end

    end

end
define_ivar(name:, value:, convenience: true) click to toggle source

Convenience method for declare_and_define_ivar()

# File lib/aastdlib/object.rb, line 41
def define_ivar(name:, value:, convenience: true)

    self.declare_and_define_ivar(name: name, value: value)

end
instance_variable_table() click to toggle source

Using terminal-table, format all instance variables in a visually appealing text-based table.

# File lib/aastdlib/object.rb, line 49
def instance_variable_table()

        rows = []
        table = Terminal::Table.new()
        table.headings = ['Instance Variable', 'Object']
        table.title = "#{self.class}: Instance Vairables"

        self.instance_variables.each do |iv|

                rows << [iv.to_s,self.instance_variable_get(iv)]
                
        end

        table.rows = rows

        return table

end
method_table(type=nil) click to toggle source

Using terminal-table, print a visually appealing table detailing methods offered by the object. Valid arguments to type include:

  • private

  • public

  • protected

# File lib/aastdlib/object.rb, line 113
def method_table(type=nil)

        mlists = {
                methods: [],
                private: [],
                protected: [],
                public: []
        }

        if type

                case type

                when :private

                        self.private_methods.sort.each  {|m| mlists[:private] << m}

                when :protected

                        self.protected_methods.sort.each {|m| mlists[:protected] << m}

                when :public

                        self.public_methods.sort.each  {|m| mlists[:public] << m}

                end

        else

                self.methods.sort.each {|m| mlists[:methods] << m} unless type
                self.private_methods.sort.each  {|m| mlists[:private] << m}
                self.protected_methods.sort.each {|m| mlists[:protected] << m}
                self.public_methods.sort.each  {|m| mlists[:public] << m}

        end

        mlists.delete_if {|k,v| v == []}

        if mlists.count < 1

                if type

                        puts "[+] No methods of #{type} type available!"

                else

                        puts "[+] No methods available!"

                end

                return

        end

        lengths = []
        headings = ['#']

        mlists.each do |k,v|
                headings << k.to_s.gsub('_',' ').capitalize
                lengths << v.length
        end

        max_len = lengths.uniq.sort.last

        table = Terminal::Table.new()
        table.headings = headings
        table.title = "Methods: #{self.class}"

        rows = []

        max_len.times do |n|

                row = []

                row << n
                mlists.each { |k,v|  row << mlists[k][n] }

                rows << row

        end

        table.rows = rows

        return table

end
pi(obj) click to toggle source

Pretty inspect and print it to stdout.

# File lib/aastdlib/object.rb, line 100
def pi(obj)

        puts
        puts obj.pretty_inspect
        puts

end
pretty_inspect() click to toggle source

Inspect the object all pretty like.

# File lib/aastdlib/object.rb, line 69
def pretty_inspect()

        tab_count = 0

        out = self.inspect.gsub("@","\n\t@")
                .lines

        expanded = ""

        counter = out.length
        (0..counter-1).each do |n|

                line = out[n]

                expanded += ("\t"*tab_count) + line

                tab_count += 1 if line =~ /\=\#\</

                if line =~ /\>\,/ and tab_count != 0

                        tab_count -= 1

                end

        end

        return expanded

end