Class: Random
- Inherits:
-
Object
- Object
- Random
- Extended by:
- RandomData::Booleans, RandomData::ContactInfo, RandomData::Dates, RandomData::Grammar, RandomData::Locations, RandomData::Names, RandomData::Numbers, RandomData::Text
- Defined in:
- lib/random_data.rb
Class Method Summary collapse
-
.method_missing(methodname) ⇒ Object
Looks for a file in the load path with the name methodname.dat, reads the lines from that file, then gives you a random line from that file.
Methods included from RandomData::Booleans
Methods included from RandomData::ContactInfo
email, international_phone, phone
Methods included from RandomData::Dates
Methods included from RandomData::Grammar
Methods included from RandomData::Locations
address_line_1, address_line_2, city, country, state_code, state_full, uk_post_code, zipcode
Methods included from RandomData::Names
companyname, firstname, firstname_female, firstname_male, full_name, initial, lastname
Methods included from RandomData::Numbers
Methods included from RandomData::Text
Class Method Details
.method_missing(methodname) ⇒ Object
Looks for a file in the load path with the name methodname.dat, reads the lines from that file, then gives you a random line from that file. Raises an error if it can’t find the file. For example, given a file named “horse.dat” in your load path: >> Random.horse
> “Stallion”
>> Random.horse
> “Pony”
>> Random.horse
> “Mare”
>> Random.horse
> “Clydesdale”
>> Random.horse
> “Stallion”
>> Random.horse
> “Mare”
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/random_data.rb', line 40 def self.method_missing(methodname) thing = "#{methodname}.dat" filename = find_path(thing) if filename.nil? super else array = [] File.open(filename, 'r') { |f| array = f.read.split(/[\r\n]+/) } return array.rand end end |