Monday, June 11, 2012

A simple Ruby snippet thing

I wrote a number of things like this to make some bits of the appearance descriptions my game go more quickly (I'm doing it in Ruby. Wheeeee~). The fact that they exist has no bearing on the poll -  which will be explained in a third post in just a moment - because all of the ideas in the poll would use the same base description system, so. I'll post some of them over time - maybe like one a day or something? I don't know.

The first one is for use with Strings.


class String # String - the base class for strings
   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
Okidoki, 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 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