Top Level Namespace
Instance Method Summary collapse
-
#args ⇒ Object
$@ -> ##args.
- #box_text(text) ⇒ Object
- #center_text(text) ⇒ Object
- #double_box_text(text) ⇒ Object
- #draw_horizontal_line(length) ⇒ Object
- #draw_vertical_line(length) ⇒ Object
-
#gcc_compile(files, project_name, extra_flags = '', override_flags = false) ⇒ Object
C compiler helpers:.
-
#line ⇒ Object
text drawing helper functions.
-
#normalize(path) ⇒ Object
usage: normalize __FILE__ normalize the directory that your scripts execute in, even if they’re running from different places.
-
#print_method_sig(method_name) ⇒ Object
for generating usage strings.
- #pyramid_text(text) ⇒ Object
-
#require_dir(directory_path) ⇒ Object
require every file in a directory.
- #right_align_text(text) ⇒ Object
-
#safe_system(command) ⇒ Object
system(command), but aborts if the command fails.
- #say_hello ⇒ Object
- #star_text(text) ⇒ Object
-
#test_all_drawing_methods ⇒ Object
Testing function.
- #triangle_text(text) ⇒ Object
-
#usage(method_name, custom_usage_string, should_exit = true) ⇒ Object
function usage string, with a custom string param that formats it for you.
Instance Method Details
#box_text(text) ⇒ Object
7 8 9 10 11 12 |
# File 'lib/drawing.rb', line 7 def box_text(text) length = text.length + 4 puts '+' + '-' * length + '+' puts "| " + text + " |" puts '+' + '-' * length + '+' end |
#center_text(text) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/drawing.rb', line 21 def center_text(text) total_length = 80 text_length = text.length leading_space = (total_length - text_length) / 2 puts ' ' * leading_space + text end |
#double_box_text(text) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/drawing.rb', line 14 def double_box_text(text) length = text.length + 6 puts '+' + '=' * length + '+' puts "|| " + text + " ||" puts '+' + '=' * length + '+' end |
#draw_horizontal_line(length) ⇒ Object
35 36 37 |
# File 'lib/drawing.rb', line 35 def draw_horizontal_line(length) puts '-' * length end |
#draw_vertical_line(length) ⇒ Object
39 40 41 |
# File 'lib/drawing.rb', line 39 def draw_vertical_line(length) length.times { puts '|' } end |
#gcc_compile(files, project_name, extra_flags = '', override_flags = false) ⇒ Object
C compiler helpers:
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/compile.rb', line 7 def gcc_compile(files, project_name, extra_flags = '', override_flags = false) line if files.empty? puts 'No files to compile' return end if project_name.empty? puts 'No project name given' return end puts "Compiling #{files} into #{project_name}..." ignore_header_warnings = '-Wno-builtin-declaration-mismatch -Wno-implicit-function-declaration' default_flags = ['-g', ignore_header_warnings].join(' ') flags = [default_flags, extra_flags].join(' ') flags = extra_flags if override_flags puts "Flags: #{flags}" command = "gcc -o #{project_name} #{files} #{flags}" # exit if the compilation fails. safe_system command puts "Compiled #{project_name}" puts "Running..." line safe_system "./#{project_name}" end |
#line ⇒ Object
text drawing helper functions.
3 4 5 |
# File 'lib/drawing.rb', line 3 def line puts '-' * 80 end |
#normalize(path) ⇒ Object
usage: normalize __FILE__ normalize the directory that your scripts execute in, even if they’re running from different places. this is useful for when you want to reference a file in the same directory as your script, but you don’t know where your script will be run from
4 5 6 7 8 9 10 |
# File 'lib/general_helpers.rb', line 4 def normalize(path) filepath = File.(path) puts filepath # now get the dirpath of this file dirpath = File.dirname(filepath) Dir.chdir(dirpath) end |
#print_method_sig(method_name) ⇒ Object
for generating usage strings.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/general_helpers.rb', line 21 def print_method_sig(method_name) method_obj = method(method_name) param_list = method_obj.parameters.map do |type, name| case type when :req then name.to_s when :opt then "#{name} = <default_value>" when :rest then "*#{name}" when :keyreq then "#{name}:" when :key then "#{name}: <default_value>" when :keyrest then "**#{name}" when :block then "&#{name}" end end puts "#{method_name}(#{param_list.join(', ')})" end |
#pyramid_text(text) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/drawing.rb', line 55 def pyramid_text(text) length = text.length (length / 2 + 1).times do |i| puts ' ' * (length - i) + text[0..(2*i)] + ' ' * (length - i) end end |
#require_dir(directory_path) ⇒ Object
require every file in a directory.
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/general_helpers.rb', line 48 def require_dir(directory_path) # Get an array of all files in the directory script_files = Dir.glob(File.join(directory_path, '*.rb')) # Iterate over each script file script_files.each do |script_file| # Require or load the script file require script_file # Or if you want to load instead of requiring: # load script_file end end |
#right_align_text(text) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/drawing.rb', line 28 def right_align_text(text) total_length = 80 text_length = text.length leading_space = total_length - text_length puts ' ' * leading_space + text end |
#safe_system(command) ⇒ Object
system(command), but aborts if the command fails.
13 14 15 16 17 18 |
# File 'lib/general_helpers.rb', line 13 def safe_system(command) unless system(command) puts "Command '#{command}' failed. Aborting." exit end end |
#say_hello ⇒ Object
2 3 4 |
# File 'lib/hello_module.rb', line 2 def say_hello puts "hello" end |
#star_text(text) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/drawing.rb', line 43 def star_text(text) length = text.length + 6 puts '*' * length puts "** " + text + " **" puts '*' * length end |
#test_all_drawing_methods ⇒ Object
Testing function
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/drawing.rb', line 63 def test_all_drawing_methods # Test the line function puts "Testing the line function:" line puts "\n" # Test the box_text function puts "Testing the box_text function:" box_text("Test text") puts "\n" # Test the double_box_text function puts "Testing the double_box_text function:" double_box_text("Test text") puts "\n" # Test the center_text function puts "Testing the center_text function:" center_text("Test text") puts "\n" # Test the right_align_text function puts "Testing the right_align_text function:" right_align_text("Test text") puts "\n" # Test the draw_horizontal_line function puts "Testing the draw_horizontal_line function:" draw_horizontal_line(10) puts "\n" # Test the draw_vertical_line function puts "Testing the draw_vertical_line function:" draw_vertical_line(10) puts "\n" # Test the star_text function puts "Testing the star_text function:" star_text("Test text") puts "\n" # Test the triangle_text function puts "Testing the triangle_text function:" triangle_text("Test text") puts "\n" # Test the pyramid_text function puts "Testing the pyramid_text function:" pyramid_text("Test text") puts "\n" end |
#triangle_text(text) ⇒ Object
50 51 52 53 |
# File 'lib/drawing.rb', line 50 def triangle_text(text) length = text.length + 2 length.times { |i| puts ' ' * (length - i) + text[0..i] } end |
#usage(method_name, custom_usage_string, should_exit = true) ⇒ Object
function usage string, with a custom string param that formats it for you.
38 39 40 41 42 43 44 45 |
# File 'lib/general_helpers.rb', line 38 def usage(method_name, custom_usage_string, should_exit = true) puts "Usage for #{method_name}:" print_method_sig(method_name) puts custom_usage_string if should_exit exit end end |