Created
July 31, 2011 17:11
-
-
Save varnie/1116976 to your computer and use it in GitHub Desktop.
An intro to Arrays usage in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
NUM_PERSONS = 100_000#10_000_000 | |
class Person < Struct.new(:sex, :age, :height, :index, :money) | |
end | |
class Array | |
def search(options={}) | |
options.delete_if {|k,v| not Person.members.include?(k.to_s)} | |
find_all { |person| | |
options.all? {|k,v| v === person.send(k) } | |
} | |
end | |
end | |
def rand_in_range(from,to) | |
rand(to+1-from) + from | |
end | |
def ask_user_input(msg, data_kind=:numeric) | |
puts msg | |
input = gets.strip.downcase | |
return nil if input == 's' | |
if data_kind == :numeric | |
if input =~ /^\d+$/ | |
input.to_i | |
elsif input =~ /^\[\d+\.\.\d+\]$/ | |
input.split('..').map(&:to_i) | |
else | |
raise ArgumentError, 'must be in the format [x..y] or just a nimeric value' | |
end | |
else | |
#suppose it is a boolean, i.e. true or false | |
raise ArgumentError, 'must be "true" or "false"' unless ['true', 'false'].include?(input) | |
input == 'true' | |
end | |
end | |
def main_loop(persons_array) | |
while true do | |
puts 'Please press `q` to quit, or press any other key to enter personal data.' | |
input = gets.strip | |
if input.downcase == "q" | |
break | |
else | |
begin | |
sex_arg = ask_user_input('sex: true or false or hit s to skip', :boolean) | |
age_arg = ask_user_input('age in the format: [x,y] or just a single value or hit s to skip', :numeric) | |
height_arg = ask_user_input('height in the format: [x,y] or just a single value or hit s to skip', :numeric) | |
index_arg = ask_user_input('index in the format: [x,y] or just a single value or hit s to skip', :numeric) | |
money_arg = ask_user_input('money in the format: [x,y] or just a single value or hit s to skip', :numeric) | |
options = {} | |
options[:sex] = sex_arg unless sex_arg.nil? | |
Hash[[:age, :height, :index, :money].zip([age_arg, height_arg, index_arg, money_arg])].each do |key, value| | |
if not value.nil? | |
options[key] = value.is_a?(Array) ? Range.new(*value) : value | |
end | |
end | |
Benchmark.bm do |b| | |
b.report("search ") do | |
result = persons_array.search(options) | |
puts "found : #{result.size} persons" | |
end | |
end | |
puts 'okay' | |
rescue ArgumentError => e | |
next | |
end | |
end | |
end | |
end | |
######################################################################## | |
srand | |
puts "generating #{NUM_PERSONS} persons" | |
persons = (1..NUM_PERSONS).map{ |person| | |
sex = rand_in_range(0,1) == 1 | |
age = rand_in_range(10, 75) | |
height = rand_in_range(160, 210) | |
index = rand_in_range(1,10) | |
money = rand_in_range(100, 1000) | |
person = Person.new(sex, age, height, index, money) | |
} | |
puts "persons #{persons.size} generated" | |
main_loop(persons) | |
puts 'bye!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment