Class Random
In: lib/random.rb
Parent: Grammar

About

Random is a class like no other. Random allows you do to fun things like ‘random.sentence’ and ‘random.position’. It takes no quarter. It asks for no parameters.

Usage

  random = Random.new

For an overview on the Grammar system, please head over to the Grammar class. These are just mainly helper functions that interact with Grammar. For example,

  random.sentence 'SENTENCE'

That would pull a random SENTENCE line out of your rules.txt, automatically replacing capitalized words with their rules.txt counterparts.

  random.item 'NOUN'

That function pulls a random NOUN out. Some other randomness might randomly be added at a random point in time.

Methods

file   item   number   percentage   phone_number   position   sentence   sleep   zip_code  

Public Instance methods

Picks a random file out of path.

  random.file 'profile_pictures/bunnies'

[Source]

    # File lib/random.rb, line 45
45:   def file path
46:     Dir[path+"/*"].random
47:   end

Generates a random item, or phrase. This is not capitalized, and is meant for things like profile interests, music, etc. Use random.sentence for sentences.

  random.item 'BAND'
  random.item 'FRUIT'

[Source]

    # File lib/random.rb, line 39
39:   def item kind
40:     replace(@rules[kind].random)
41:   end

A random number of specified length. Used for zip codes, phone numbers, etc

  random.number 10

[Source]

    # File lib/random.rb, line 63
63:   def number length=1
64:     Array.new(length).collect{rand(10)}.join
65:   end

A skewed percentage between 20 and 75.

  random.percentage

[Source]

    # File lib/random.rb, line 57
57:   def percentage
58:     20+rand(55)+rand
59:   end

A random ten digit phonenumber, yeehaw!

  random.phone_number

[Source]

    # File lib/random.rb, line 75
75:   def phone_number
76:     number 10
77:   end

Picks a (skewed) random position array [x,y]. Used for tagging photos at a random spot.

  random.position

[Source]

    # File lib/random.rb, line 51
51:   def position
52:     [percentage.to_s[0..5],percentage.to_s[0..5]]
53:   end

Generates a random sentence. The parameter it takes is the reference to the Grammar rules entry

  random.sentence 'REMARK'
  random.sentence 'SENTENCE'

[Source]

    # File lib/random.rb, line 21
21:   def sentence start='REMARK'
22:     replace(@rules[start].random).capitalize
23:   end

Sleeps for a random amount of time between min and max

  random.sleep 15,30

[Source]

    # File lib/random.rb, line 27
27:   def sleep(min=30,max=45)
28:     raise 'max cannot be less than min' if max < min
29:     
30:     seconds_to_sleep = (60*min + rand(60 * (max-min)))
31:     puts "(#{Time.now.strftime("%H:%M")}) Sleeping for #{seconds_to_sleep/60} mins"
32:     Kernel.sleep seconds_to_sleep
33:   end

A random ten digit zipcode, yeehaw!

  random.zip_code

[Source]

    # File lib/random.rb, line 69
69:   def zip_code
70:     number 5
71:   end

[Validate]