I am developing a little application within instantrails, suppose to work in an intranet environment.

The application let user book a part number, by filling some information. For better user experience, I plan to capture the username at the other end that make the booking, instead of creating another layer of login feature and require the user to login again. There is no complex authentication needed, just capture the username and store it together with other information.

ENV['user'] will give you the username that has the rails application running, which is not what we want.
ENV['remote_user'] return nothing at all.
I read through quite a few discussions on the net, it seems like apache didn’t actually pass the remote_user value to mongrel, hence rails application couldn’t call out remote_user. To fix that, we will need to modify on the apache configuration file, httpd.conf, and .htaccess. Admittedly, I really know close to nothing about the setup of these files, and have a hard time following the suggested steps, as to where to add the suggested line of codes, which file to modify, etc, etc.

So I figure, if we forgo apache in the setup, will it be easier to fetch the remote_user value directly from mongrel? or from thin if it is the web server being used.

But still, I just couldn’t get it working.
Is there any gem or plugin need to be installed before this remote_user tingy can be pass to rails application?

thin on windows

ruby on rails

Read about thin for a while (faster than webrick, faster than mongrel), but didn’t really get the chance to try it out, until recently.

As I am looking for moving my little rails application out of instantrails to a proper production environment, decided to give thin a spin, too bad Passenger isn’t supporting Windows platform, heard a lot of good thing about it too.

Installing thin server on Windows isn’t quite straightforward though. Thin actually consist of mongrel parser, EventMachine, and rack, but the latest EventMachine gem is not win32 binary release, that pose a lot of problem for the installation.

There are a few workarounds suggested on the net,
from Winston

  • install rack with the usual gem install
  • install EventMachine gem 0.8.1 as local gem
  • install thin gem install thin –ignore-dependencie
  • But this didn’t quite workout for me, some suggested to install MinGW and MSYS, then the normal path of gem install thin should just work. Tried that, but fail as well.

    Then found out that EventMachine has a binary release at 0.12.6, and with that it works right away for me :-) , here are the steps that did it for me,

  • gem install rack
  • gem install eventmachine-0.12.6-x86-mswin32-60.gem (download gem to local, and install locally)
  • gem install thin
  • Only after a full circle of searching around the net, that I found out these steps are actually mentioned in the comments by Marty McGee in winston’s original post. Duh.

    bubble sort

    ruby

    Understanding sorting algorithm by just reading text or still illustration takes a lot of mind gymnastic. Apparently, there are lot of videos online, trying to explain the concept. But not all explain it as good though, some with really boring animation, some with crappy background music, and some even both, ;-) . Here are two that I find did a great job in explaining bubble sort,

    This one from codegearguru. Instead of creating graphic, or cutting out paper to represent data in the sorting process, just use the poker cards! That seems obvious now, but I didn’t think about it when I try to illustrate sorting to myself initially. Ha. Think there is a series of video created for different type of sorting algorithm as well, so definitely a great tool to understand sorting algorithm. Bonus near the end of the video, another technique called shaker sort or cocktail sort is also explained, which is basically a bidirectional bubble sort which should speed up the sorting speed.

    Another one from Miles Hauskaz, just plain simple and succinct explanation and a video nicely done as well.

    Sorting Algorithms – Bubble Sort from Miles Hauskaz on Vimeo.

    So, this is my implementation of bubble sort, admittedly not the best that you can find, but that will do for me now, until I have time to come back and do some refactoring.

    arr = %w{g a c b j e e}
    i = arr.length - 1
    count = 0
    n = arr.length - 1
    def swap(m,n) 
      arr = []
      if m > n
        arr << n
    	arr << m
      else
        arr << m
    	arr << n
      end
    end
     
    while n > 0
      while count < i
        puts "comparing #{arr[count]} to #{arr[count+1]}"
        a = swap(arr[count],arr[count+1])
        arr[count..count+1] = a
        count += 1
      end
      n -= 1
      i -= 1
      count = 0
    end
    p arr

    map and each

    ruby

    It is a bit difficult for me to understand the difference between map and each, until I found out this in irb accidentally,

    irb(main):001:0> name = %w{guido knuth adrian dhh pg}
    => ["guido", "knuth", "adrian", "dhh", "pg"]
    irb(main):002:0> name.each {|n| n.upcase}
    => ["guido", "knuth", "adrian", "dhh", "pg"]
    irb(main):003:0> name.map {|n| n.upcase}
    => ["GUIDO", "KNUTH", "ADRIAN", "DHH", "PG"]

    map and each are suppose to do almost the same thing, but why each didn’t return the correct result in uppercase like what map did?

    The different is each yield each element in the collection to the code block, in this case perform the upcase method, but return the receiver, which is the original array, hence an array of lower case names.

    Whereas, map return a new array, consists of elements already went through the code block, hence an array of upper case names.

    irb(main):007:0> name.each {|n| p n.upcase}
    "GUIDO"
    "KNUTH"
    "ADRIAN"
    "DHH"
    "PG"
    => ["guido", "knuth", "adrian", "dhh", "pg"]
    irb(main):006:0> name.map {|n| p n.upcase}
    "GUIDO"
    "KNUTH"
    "ADRIAN"
    "DHH"
    "PG"
    => [nil, nil, nil, nil, nil]

    So here you can see with p, both map and each output the uppercase names, but look more carefully, the return value of each, as expected, is the original array. map however, return an array of nils!

    Why is that?

    A much better sort

    ruby

    Okay, much better only in a relative term, comparing to my earlier sorting which didn’t take care of duplicate elements.
    Two major changes here.

    • I do away with the temporary variable for holding on the smallest number result from each round of the comparison, instead elements are now passed over directly to the sorted array.
    • Those elements that get passed to the sorted array, will be deleted from the original array, but one items at one round of comparison, which mean if there are duplicated elements, only one of the instance will be removed.
    •  hackers.delete_at(hackers.index(sorted[count]))

      It look a lot more wordy than a shorter

      hackers.delete(sorted[count])

      but this is deleting ONLY the first element that match the value, compare to the shorter version where it will remove all elements. So it preserve the other element for next round of comparison, and eventually make it to the sorted array.
      Complete code as below,

      hackers = %w{ gosling matz dhh adrian guido matz knuth dhh adrian guido }
      i = hackers.length
      count = 0
      sorted = []
       
      while count < i
        sorted[count] = hackers.inject{ |a,b| a < b ? a : b }
        p sorted[count]
        hackers.delete_at(hackers.index(sorted[count]))
        count += 1
      end
      p sorted

      And the result….

      ["adrian", "adrian", "dhh", "dhh", "gosling", "guido", "guido", "knuth", "matz", "matz"]

    Finally have the time to come back to the sorting exercise, here is a slightly better version of sort.
    It still doesn’t take care of duplicate elements, but compare to the first sorting version, this one is shorter, as the whole finding smallest element left in the array is now done by inject and a ternary operator, a whole 7 lines of code cut to just 1. And now it no longer sort animals, it sort a list of hackers :-)

    hackers = %w{matz ddh adrian guido gosling knuth linus}
    i = hackers.length 
    sorted = []
    while i > 0
      small = hackers.inject{ |a,b| a < b ? a : b}
      sorted.push small 
      hackers.delete small
      i -= 1
    end
    p sorted

    And the result of the sort is

    ["adrian", "ddh", "gosling", "guido", "knuth", "linus", "matz"]

    So the next iteration will be to take care of duplicated elements…stay tuned.

    Apparently you can have optional parameter listed in the middle for your method in Ruby version 1.9

    C:\>pik switch 191
     
    C:\>irb
    irb(main):001:0> def talk(a,*b,c)
    irb(main):002:1>   p a,b,c
    irb(main):003:1> end
    => nil
    irb(main):004:0> talk 'a','b','c'
    "a"
    ["b"]
    "c"
    => ["a", ["b"], "c"]

    but not Ruby version 1.8.7

    C:\>pik switch 187
    C:\>irb
    irb(main):001:0> def talk(a,*b,c)
    irb(main):002:1>   p a,b,c
    irb(main):003:1> end
    SyntaxError: compile error
    (irb):1: syntax error, unexpected tIDENTIFIER, expecting tAMPER or '&'
    def talk(a,*b,c)
                   ^
            from (irb):1
    irb(main):004:0> quit

    A beautiful visualization of sorting algorithm

    via Hacker News

    Another way for factorial, extending the integer class, so the function will feel more natural, instead of fact 6 for example, you just call it by 6.fact.
    Also, added a function to sum up the factorial value, counting down. Example, for 6!, it will be 6! + 5! + 4! + 3! + 2! + 1!
    Here go,

    class Integer
      def fact
        if self == 0
          return 1
        else
          n = self * (self - 1).fact
        end
      end
      def sum_fact
        (1..self).inject{|a,b| a + self.fact}
      end
    end
    puts 4.fact
    puts 4.sum_fact

    failing factorial

    ruby

    Hours ago I got a programming test question about factorial, which I didn’t do really well, only completed with the help of the interviewer. Damn.
    The worst thing is that I have done this question before, and I know in the head I can use recursion for it, but I totally go blank on how to do it with recursion. Here go my chance…sigh…

    Now, here is how factorial can be done.
    With Recursion.

    def fact n
      if n == 0
        1
      else
        n = n * fact(n-1)
      end
    end

    That simple.

    Factorial without recursion, using inject instead

    def fact n
      if n == 0
        1
      else
        (1..n).inject { |a,b| a*b } 
      end
    end

    Isn’t that difficult, huh? What was I thinking?!