Login Register


Ruby List Method - Simple: The Ruby Library filter_list
Author
Message
Ruby List Method - Simple: The Ruby Library #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.

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 end

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:
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.') end

Not to mention this example has no error handling, it simple puts them out as they happen.

Reply







Users browsing this thread: 1 Guest(s)