Class: RubyTutor
- Inherits:
-
Object
- Object
- RubyTutor
- Defined in:
- lib/rubytutor.rb,
lib/rubytutor/version.rb
Overview
RubyTutor main class
Constant Summary collapse
- VERSION =
"0.1.0"
Class Method Summary collapse
- .available_methods(object, filter_str = nil) ⇒ Object
- .construct_explain(object, class_name, ancestors, value) ⇒ Object
- .describe(object) ⇒ Object
- .explain(object) ⇒ Object
- .explain_full(object) ⇒ Object
- .extract_contents(files, description, object, class_name) ⇒ Object
- .find(files, class_name) ⇒ Object
- .full_explanation(object) ⇒ Object
- .need?(object, method_symbol) ⇒ Boolean
- .retrieve_class(object) ⇒ Object
- .retrieve_description(object) ⇒ Object
- .retrieve_explanation(object) ⇒ Object
- .retrieve_methods(class_name, filter_str = nil) ⇒ Object
- .valid?(letter) ⇒ Boolean
-
.variable_text(object, text) ⇒ Object
Not pretty, but this is the method that determines which strings will go Into each ‘explain’ or ‘explain_full’ call.
Class Method Details
.available_methods(object, filter_str = nil) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/rubytutor.rb', line 17 def self.available_methods(object, filter_str = nil) class_name = object.class == Class ? object : object.class methods = retrieve_methods(class_name, filter_str) puts methods end |
.construct_explain(object, class_name, ancestors, value) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/rubytutor.rb', line 48 def self.construct_explain(object, class_name, ancestors, value) text = [] text << "Instance of Class: #{class_name}" text << "Value: #{value}" variable_text(object, text) text << "Mutable? #{object.frozen? ? 'No' : 'Yes'}" text << "Object ID: #{object.object_id}" text << "Inhertits From: #{ancestors}" text << '' end |
.describe(object) ⇒ Object
9 10 11 |
# File 'lib/rubytutor.rb', line 9 def self.describe(object) puts retrieve_description(object) end |
.explain(object) ⇒ Object
13 14 15 |
# File 'lib/rubytutor.rb', line 13 def self.explain(object) puts retrieve_explanation(object).join("\n") end |
.explain_full(object) ⇒ Object
5 6 7 |
# File 'lib/rubytutor.rb', line 5 def self.explain_full(object) puts full_explanation(object).join("\n") end |
.extract_contents(files, description, object, class_name) ⇒ Object
103 104 105 106 107 108 109 110 111 |
# File 'lib/rubytutor.rb', line 103 def self.extract_contents(files, description, object, class_name) files.each do |file_path| File.open(file_path) do |file| file.each do |line| description << format(line, value: object, class: class_name) end end end end |
.find(files, class_name) ⇒ Object
113 114 115 116 117 118 119 120 |
# File 'lib/rubytutor.rb', line 113 def self.find(files, class_name) class_file = File.("../descriptions/#{class_name}.txt", __FILE__) blank_file = File.('../descriptions/blank.txt', __FILE__) files << File.('../descriptions/intro.txt', __FILE__) files << (File.file?(class_file) ? class_file : blank_file) files << File.('../descriptions/last.txt', __FILE__) end |
.full_explanation(object) ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/rubytutor.rb', line 77 def self.full_explanation(object) full_string = [] full_string << retrieve_explanation(object) full_string << retrieve_description(object) full_string.flatten end |
.need?(object, method_symbol) ⇒ Boolean
73 74 75 |
# File 'lib/rubytutor.rb', line 73 def self.need?(object, method_symbol) object.respond_to?(method_symbol) end |
.retrieve_class(object) ⇒ Object
99 100 101 |
# File 'lib/rubytutor.rb', line 99 def self.retrieve_class(object) object.respond_to?(:members) ? Struct : object.class end |
.retrieve_description(object) ⇒ Object
85 86 87 88 89 90 91 92 93 |
# File 'lib/rubytutor.rb', line 85 def self.retrieve_description(object) class_name = retrieve_class(object) description = ["Description:\n"] files = [] find(files, class_name) extract_contents(files, description, object, class_name) description.join end |
.retrieve_explanation(object) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/rubytutor.rb', line 40 def self.retrieve_explanation(object) class_name = retrieve_class(object) ancestors = class_name.ancestors[1..-1].join(', ') value = object.nil? ? 'nil' : object construct_explain(object, class_name, ancestors, value) end |
.retrieve_methods(class_name, filter_str = nil) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/rubytutor.rb', line 26 def self.retrieve_methods(class_name, filter_str = nil) method_names = class_name.methods.sort if valid?(filter_str) last_index = filter_str.length method_names = method_names.map(&:to_s).select do |method| method[0...last_index] == filter_str end end "Available Methods: \n" + method_names.join("\n").to_s + "\n\n" end |
.valid?(letter) ⇒ Boolean
95 96 97 |
# File 'lib/rubytutor.rb', line 95 def self.valid?(letter) letter.respond_to?(:match) ? letter.downcase.match(/[a-z]/) : false end |
.variable_text(object, text) ⇒ Object
Not pretty, but this is the method that determines which strings will go Into each ‘explain’ or ‘explain_full’ call
64 65 66 67 68 69 70 71 |
# File 'lib/rubytutor.rb', line 64 def self.variable_text(object, text) text << "Return Value: #{object.call}" if need?(object, :call) text << "Source Value: #{object.source}" if need?(object, :source) text << "Members: #{object.members.join(', ')}" if need?(object, :members) text << "Keys: #{object.keys.join(', ')}" if need?(object, :keys) text << "Values: #{object.values.join(', ')}" if need?(object, :values) text << "Length: #{object.length}" if need?(object, :length) end |