The first one is for use with Strings.
class String # String - the base class for stringsOkidoki, since it's written up there so... well, not really nicely, since the format of it was utterly murdered as far as I'm concerned and I had to poke it some to make it look even mildly as nice as it does... but well, it's written up there, so anyway, using it. Say I'm going to be using a noun generated by a method, but I don't know what noun it'll be until the method runs. I can't put a static "a" down because the noun might start with a vowel, and I can't use "an" because it might start with a consonant. Here's our example method:
def aan # Aan - adds either a or an, depending on what the first letter in the string is.
vowels = ["a", "e", "i", "o", "u"]
if vowels.include?(chr.downcase)
string = " an " + self
else
string = " a " + self
end
return string
end # end aan
end
def testmethod
nouns = ["ice cream cone", "frog"]
return nouns[rand(nouns.size-1)]
end
puts "I want a " + testmethod
Okay, so we have an even chance of getting either "I want a ice cream cone" or "I want a frog" at the end of the displayed string. The problem here is that "a ice cream cone" is not proper English. If my aan method is defined, then we could use this instead:
puts "I want " + testmethod.aan
This would return either "I want an ice cream cone" or "I want a frog." Much better!
So far I've used this snippet to mention the basics of the face of characters - are they ordinary? Feline? Canine? Stuff like that. Thanks to the snippet, they have "an ordinary face," "a feline face," "a canine face," etc. with no extra work on my part. Yay.
No comments:
Post a Comment