Skip to content

Instantly share code, notes, and snippets.

@kennyt
Last active December 10, 2015 22:08
Show Gist options
  • Save kennyt/4499711 to your computer and use it in GitHub Desktop.
Save kennyt/4499711 to your computer and use it in GitHub Desktop.
##
## I stringed together all the parts of code I commented on.
## In general, space your code more!
def my_sort
self.size.times do |variable|
self.each_with_index do |element, index|
unless self[index+1].nil?
result = yield(element, self[index+1]) # adding a default i.e. proc.nil? {|x,y| x<=>y} instead of yield would make it more sort-like
if result==1 || result == true # I don't understand. Isn't everything other than nil true? Therefore result == 1 adds nothing.
temp = self[index+1]
self[index+1] = element
self[index] = temp
end
end
end
end
self
end
def binary_search_iterative(input_array, target)
start_point = 0
end_point = input_array.length - 1
mid_point = (start_point + end_point) / 2
counter = 0
#spacing at certain points can make your code much easier to read: like here
while input_array[mid_point] != target
if counter > input_array.size
raise "Target not found in input array."
end
#here
if input_array[mid_point] > target
end_point = mid_point
mid_point = (start_point + mid_point) / 2
else
start_point = mid_point
mid_point = (mid_point+1 + end_point) / 2
end
#here
counter += 1
end
## most of your code is like this. It'd be nicer for the reader if you had spaces :)
class XmlDocument
def initialize(indent = false)
@indent = indent
@nest = 0
end
def method_missing(method_name, *args, &proc)
if proc.nil?
if args.empty?
"<#{method_name}/>"
else
local_nest=@nest
#this ternary can use a comment
@indent ? "#{' '*local_nest}<#{method_name} #{args[0].keys.join}='#{args[0].values.join}'/>\n" :
"<#{method_name} #{args[0].keys.join}='#{args[0].values.join}'/>"
end
else
local_nest = @nest
@nest+=1
#this one too
@indent ? "#{' '*local_nest}<#{method_name}>\n#{yield}#{' '*local_nest}</#{method_name}>\n" :
"<#{method_name}>#{yield}</#{method_name}>"
end
end
end
class Coin
def initialize(value, remainder, prev_coin)
@value = value
@remainder = remainder
@prev_coin = prev_coin
end
attr_accessor :value, :remainder, :prev_coin #move to beginning of class
end
def print_fibonacci(size)
puts "Which type? (Recursive = 1, Iterative = 2)"
type = gets.chomp # nice one on letting the user choose. cool
fib_array = []
(1..size).each do |s|
if (type == "1")
fib_array << fib_recursive(s) # could use size_number or another name instead of s to understand what s is
else
fib_array << fib_iterative(s)
end
end
p fib_array
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment