Ruby List Method - Simple: The Ruby Library 04-15-2014, 12:29 PM
#1
So I've been developing a Ruby library to speed up as well as clean up my Code and well here is my list() method, it can take direct parameters or an array and display them as a list. It works with a number of other Methods i have built.
An example of this method in use, I've also used some of my other library methods to show just how clean and organised it all looks together:
Also works like: list('John','David','Alice', '+')
How it would look,
Not to mention this example has no error handling, it simple puts them out as they happen.
Code:
def list(*simple_args)
if !simple_args[0].kind_of?(Array)
if simple_args.length > 0
simple_get_last_list_item = simple_args.length - 1
simple_count = 1
for simple_list_item in simple_args
if simple_count < simple_args.length
puts simple_args[simple_get_last_list_item] + " " + simple_list_item
simple_count += 1
else
break
end
end
else
puts("\nSimple Error: List, must pass at least 2 parametes (List item, formate sytle.) or Array and formate style.")
end
else
if simple_args[0].length > 0
simple_get_last_list_array_item = simple_args[0].length - 1
simple_count = 0
for simple_list_array_element in simple_args[0]
if simple_count < simple_args[0].length
puts simple_args[1] + " " + simple_args[0][simple_count]
simple_count += 1
else
break
end
end
else
puts("\nSimple Error: List, must pass at least 2 parametes (List item, formate sytle.) or Array & formate style.")
end
end
return
endAn example of this method in use, I've also used some of my other library methods to show just how clean and organised it all looks together:
Code:
load 'simple.rb'
names = ['John','David','Alice']
if is_array(names)
list(names, '+')
else
simple_error('In this instance array must be passed.','Simple Error')
end
display_errors('all')Also works like: list('John','David','Alice', '+')
How it would look,
Code:
names = ['John','David','Alice']
if names.kind_of?(Array)
names.each_line do |element|
puts('+' + element)
end
else
puts('Simple Error: In this instance array must be passed.')
endNot to mention this example has no error handling, it simple puts them out as they happen.
![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)