Archive for the ‘Ruby’ Category

It took a while to find this, but here’s my solution

Add this file: /Library/LaunchDaemons/org.postgres.launchd.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
        "http://www.apple.com/DTDs/PropertyList-1.0.dtd";>
<plist version="1.0">
<dict>
    
    <key>Label</key>
      <string>org.postgres.launchd</string>
    <key>Disabled</key>
      <false/>
    <key>UserName</key>
      <string>_pgsql</string>
    <key>GroupName</key>
      <string>_pgsql</string>
    <key>Program</key>
      <string>/usr/local/pgsql/bin/postmaster</string>
      <key>EnvironmentVariables</key>
      <dict>
              <key>PGDATA</key>
              <string>/usr/local/pgsql/data/</string>
      </dict>
    <key>RunAtLoad</key>
      <true/>
</dict>
</plist>

You can then load it and unload it by issuing:

$ sudo launchctl load /Library/LaunchDaemons/org.postgres.launchd.plist
$ sudo launchctl unload /Library/LaunchDaemons/org.postgres.launchd.plist

Now get to making some great Rails stuff!

Product Announcement

Sunday, January 11th, 2009

My blogging has been light for the last several months because I’ve been focused on trying to get admitted to graduate school. Included in the portfolio of “why Steven is a special snowflake and you should admit him” argumentation is reference to a project which demonstrates the fusion of my interests in Latin, linguistics, Ruby, metaprogramming, and Rails.

To find out more about my project, visit The Verba Latina website. As a warning, the initial load is a bit pokey. I’m going to move to a more robust host later.

LatinIRB

Sunday, October 5th, 2008

As I mentioned previously, I’ve been working on a library in Ruby used to model Latin Verbs.

Here’s a 5 minute demo of what you can do: My Latin::LatinVerb + Ruby’s IRB environment produces this:

It’s easier on the eyes if you watch the full-size version at blip.tv

I’m working on building a site around this code base. I hope to release it within the next month, but for the moment, I can give you a small sample of my recondite study.

Life, not blogging

Sunday, September 21st, 2008

I’ve been taking a bit of a hiatus from blogging thanks to:

  1. Car accident drama
  2. Latin III
  3. postponed GRE from this weekend to the 24th of October
  4. Work
  5. The LatinVerb library.

Most of these don’t produce much in the way of visual artifacts, but here’s a short demonstration of the LatinVerb library ( it’s coherently working, but still needs some refactoring to get it to 0.1 release state… ). Here’s the video:

What’s going on here is that I have a Ruby debugger ( RDebug ) session open where I’ve created an instance of Latin::LatinVerb. I then proceed to execute some of the “vectors” that uniquely identify a single conjugation of a verb or a collection of verb tenses.

It’s not too pretty yet, my main work has been on getting the thinking working. The displaying part comes next.

Ruby Debugger Things I Always Forget

Sunday, August 24th, 2008

Continuing on a theme seen about Perl. Basic commands are not covered ( list , print, continue, etc. )

  1. Install rdebug: sudo gem install rdebug -y

  2. Open a session: rdebug script

  3. Always show a listing after every command: set autolist

  4. Show a variable consistently: display variableName

  5. …or the result of a method on a variable: display variableName.methodName

  6. Conditional break…conditionally: break ./fileName:lineNumer [if condition]

  7. See where you are in the stack ( especially handy if you like recursion, like me ): where

  8. Move up/down in the stack: up/down

  9. Go to the nth frame in the stack: frame n

    (more…)

OK I said that you conjugate the verb fully, but that’s not entirely true. As I’ve not yet learned the subjunctive system, I’m limited to the indicative present and perfect systems. I’ve also included the participles as well as the infinitives.

Might I add that linguistic and string processing with Ruby is a pleasure. Many Years (tm) ago I tried this is French with Java and it was an unholy pain.

Can anyone guess where I’m going with these last few posts?

  • Simplified macron-based entry ( \={a}, \={e}, etc.)
  • Process simplified text to UTF-8 ( the language of rails )
  • Code to process these strings into full conjugations
  • Rails as a display engine

What do you think I’m making?

BTW: This code is very much rough draft, it’s enough to handle the ideas, but needs more modularization and more refactoring, so this is not “steven at his best” level code

(more…)

When I write my Latin homework in LaTeX, the source winds up looking like this:

\item[13.] Am\={\i}c\={o}s tr\={\i}st\={e}s exc\={e}pit, ad m\={e}nsam inv\={\i}t\={a}vit, et e\={\i}s perfugium ac s\={o}l\={a}cium h\={\i}c dedit.

{ Aside: You might be thinking that entering “\ = { \ i }” just to get a single character would be a drag, but thanks to Textmate I have created snippets such that Control + letter does all that typing for me }

Now if I want to post this to the web, I need to convert those characters from say “\={\i}” to “\ī”. I would like it to go through the sentence and change each of those LaTeX-macron characters to HTML entities.

Similarly, I occasionally have need to convert LaTeX-macron sequences to UTF-8 codes.

And yet other times I want it to convert those codes to macron-ized characters.

So I wrote a script that takes input of the LaTeX form and will let you specify if you want the output in HTML entity (default), macron characters, or utf8 characters.

The best part is that I’ve written this function into Textmate as an extension so for me conversion is:

Default String

ss_tmlatinconv_1

Highlight the LaTeX string

tm_latinconv_2

Choose my command with this script in it

ss_tmlatinconv_3

Text is replaced, magically!

ss_tmlatinconv_4

And it looks like this in a browser…

\item[13.] Amīcōs trīstēs excēpit, ad mēnsam invītāvit, et eīs perfugium ac sōlācium hī

Previously I had shown how I had used Ruby to re-create passive system conjugations. I decided I’d try to add on some functionality and I’ve come up with a Ruby script that handles full verbal system conjugation for 1st and 2nd conjugations (-āre / -ēre) in Latin.

I’ll ultimately build this into a Rails application, but I’m getting the design and debug out of the way with a ruby script.

Check out the code on my tracking page. I need to add support for 3, 3-io, and 4th, but I’m amazed by how light this code has come up to be ( ~ 300 lines with generous whitespace! ).

I’m pretty pleased with my recursive macron validation scheme. Showing off recursive code ( for non Lisp people ) is sorta like being a white guy demonstrating your breakdancing skills. You’re embarrassed to try, you’re embarrased with the result, but even if you do it a little bit, you feel way proud of yourself ( probably too much ).

My printfs aren’t aligning as you see in the output. If there are any printf gurus that can help me let me know.

This started off as a bit of an amusement just to see if i could show the elegance of Ruby — and find a way to help me memorize the suffix-addition-heuristics that characterize inflected languages like Latin.

As usual, it quickly became much more than that.

Known Issue:

The macrons are slightly off, particularly in the 3rd person plural ( no vowel before ‘nt’ should ever be lengthened ). I’ll write a fixer routine soon.

[code lang="ruby"]
#!/usr/bin/env ruby
class Verb < Object
  attr_reader :firstpersonform, :infinitive, 
              :perfectstem, :passperfpart, :stem
  def initialize(initString)
    (@firstpersonform, @infinitive, 
       @perfectstem, @passperfpart)=initString.split(/,\s+/)
    pparts=initString.split(/,\s+/)
    @passive_endings = ["r", "ris", "tur", 
                        "mur", "min\xc4\xab", "ntur"]
    @personages = ["First Singular", "Second Singular", 
                   "Third Singular",
                   "First Plural", "Second Plural", "Third Plural"]
  end
  def stem
    # For efficiency, if the iVar @stem is defined, don't go 
    # through this structure
    return @stem unless @stem.nil?
    if @infinitive =~ /?re$/
      return @stem = @infinitive.gsub(/(.*)?re$/,'\\1?')
    end
    if @infinitive =~ /?re$/
      return @stem = @infinitive.gsub(/(.*)?re$/,'\\1?')
    end    
  end
  def to_s
    return "#{self.firstpersonform} #{self.infinitive} 
       #{self.perfectstem} #{self.passperfpart}"
  end
  def present_passive
    local_pe=@passive_endings.clone
    return [@firstpersonform + "r", 
      local_pe[1..-1].map{|x| self.stem + x}].flatten!
  end
  def imperfect_passive
    imperfect_stem = self.stem + "b\xc4\x81"
    return @passive_endings.map{|x| "#{imperfect_stem}#{x}"}
  end
  def future_passive
    fp_stem=self.stem+"bi"
    standards = @passive_endings[2..-1].map{|x| fp_stem + x}
    return [@stem + "b\xc5\x8d", @stem + "beris", standards].flatten!
  end
  def passive_system
    p_sys_hash= { :label=>@personages,
                  :present => self.present_passive,
                  :imperfect => self.imperfect_passive,
                  :future => self.future_passive}
    0.upto(5) do |index|
      printf("%-15s %-10s %-16s %s\n", 
                      p_sys_hash[:label][index],
                       p_sys_hash[:present][index],
                       p_sys_hash[:imperfect][index],
                       p_sys_hash[:future][index]
                      )
    end
    puts "\n\n"
  end
end
[/code]
    x=Verb.new("laudō, laudāre, laudāvī, laudatus")
    puts x
    x.passive_system

    y=Verb.new("moneō monēre, monuī, monitus")
    puts y
    y.passive_system

And the output?

    laudō laudāre laudāvī laudatus
    First Singular  laudōr    laudābār       laudābō
    Second Singular laudāris  laudābāris     laudāberis
    Third Singular  laudātur  laudābātur     laudābitur
    First Plural    laudāmur  laudābāmur     laudābimur
    Second Plural   laudāminī laudābāminī   laudābiminī
    Third Plural    laudāntur laudābāntur    laudābintur


    moneō monēre monuī monitus
    First Singular  moneōr    monēbār        monēbō
    Second Singular monēris   monēbāris      monēberis
    Third Singular  monētur   monēbātur      monēbitur
    First Plural    monēmur   monēbāmur      monēbimur
    Second Plural   monēminī monēbāminī    monēbiminī
    Third Plural    monēntur  monēbāntur     monēbintur

I don’t think that you have to be a Ruby guru to see that Ruby code is incredibly tight. Interestingly enough, Latin, as according to the gospel of Wheelocki, is taught in a very programmatic heuristic manner. You’re taught to carry a certain data file of critical givens on your biological hard drive ( a verb has 4 principal parts, the principal parts of “to praise” …. ) and then are taught a series of transformations to be performed on those 4 principal parts. Working those out is an exercise being a mental computer, mutating data according to heuristic and spitting it out.

Interestingly, when printing technology was much more expensive ( or, students were much more rebellion-prone when forced to pay outrageous prices for books ) Latin instruction was a few terse rules and the homework was “Write out the application of these heuristics to the following data items”. With the rules, and the given inputs, the human scribbler would write out the references he would need to be able to advance to the next chapter.

I have a certain admiration for this method.

But I also have ruby, and remembering how i applied a few map()s or flatten!() statements is just as good.

[code lang="ruby"]
irb(main):001:0> "capture to here!  leave this off".gsub(/(.*\!).*(\s+.*)$/, "\\1 and \\2")
=> "capture to here! and  off"
[/code]

I looked in my Pickaxe, I looked in various tutorials, but this simple little thing hung me up more than many other Ruby questions I’ve had over the years.