Sunday, September 18, 2011

More interpretation required for interpreted languages

After I finished ruby koans (and they are absolutely awesome). I started to look at python koans. In the very beginning I realized that there is something extremely silly about programming languages, you still have to write things like below (to avoid introducing not needed empty spaces):
        string = "It was the best of times,\n\
It was the worst of times."

It is not the same as
  
        string = "It was the best of times,\n\
                  It was the worst of times."

Same problem is displayed in ruby koans:
    long_string = %{
It was the best of times,
It was the worst of times.
}

There are times when this is extremely inconvenient, and your style can only be saved by using templates as in:
require "erb"

#test.erb:
# hubba
# hubba <%= oink %>

erb = ERB.new(File.read("test.erb"))
oink = "hubba"

puts erb.result

Friday, May 7, 2010

passenger install

passenger-install-apache2-module -a 2>&1 |grep -E "(LoadModule|PassengerRoot|PassengerRuby)"

If you are tired of it telling you what you already know.

Friday, March 5, 2010

switching between ruby 1.8 and 1.9

So if you followed homebrew + ruby 1.9 + rails 3.0 instructions from the previous post,  you now have ruby 1.9.x in your /usr/local/bin, and ruby 1.8.x in your /usr/bin. Same applies to gems, and thus rails.
Let's say you want switch environments, how do you do that?

Well it's simple. You want to put either /usr/bin or /usr/local/bin in front in your shell's $PATH variable. And you can do this with simple aliases:


alias ruby19="export PATH=/usr/local/bin:/usr/bin:`echo $PATH| sed -E 's/\/usr(\/local){0,1}\/bin[:]{0,1}//g'`"
alias ruby18="export PATH=/usr/bin:/usr/local/bin:`echo $PATH| sed -E 's/\/usr(\/local){0,1}\/bin[:]{0,1}//g'`"


now you can switch simply by running ruby18 or ruby19 in a terminal windows, to make it stay, just add these lines to your ~/.bash_profile

Thursday, February 11, 2010

Snow Leopard + Ruby 1.9 + Rails 3.0 beta + homebrew


While my PS3 downloads Heavy Rain demo I want to take a moment to write about my recent discovery: homebrew. Homebrew is yet another package manager for os x, but it is ruby based (which is immensely cool, for all the lovers of esthetics), keeps /usr and /usr/local user-space unpolluted with crap, and is meant to be fast and simple (aren't they all?). There is something so special about homebrew (which I didn't even really used) and its idea that forced me to completely reinstall my macbook to get rid of all the macports/fink/leopard legacy. But back to the subject.

To install homebrew, you need to have Xcode installed. You can get it from http://developer.apple.com/tools/xcode/ or your Snow Leopard installation dvd. To summarize the instruction on homebrew page you need to run this commands in terminal:












sudo chown -R `whoami` /usr/local
curl -L http://github.com/mxcl/homebrew/tarball/master | tar xz --strip 1 -C /usr/local

    If you don't have '/usr/local', just run 'sudo mkdir /usr/local' and repeat the steps above. Now, people will argue that that setting user ownership on /usr/local is not secure, but it's not really true. You can always change it, and it doesn't do anything with admin permissions, where the real security lies. After you do this you need to set your PATH environment variable to begin with /usr/local/bin.

    for bash:




    
    
    
    
    export PATH=/usr/local/bin:$PATH

      for csh/tcsh:




      
      
      
      
      setenv PATH /usr/local/bin:$PATH
      You probably want to add it to your shell profile, so do 





      
      
      
      
      cat 'export PATH=/usr/local/bin:$PATH' >> ~/.bash_profile

        or




        
        
        
        
        cat 'setenv PATH /usr/local/bin:$PATH' >> ~/.tcshrc
        From this moment I will assume that you are a bash user (but it doesn't really matter), which is default shell in os x:




        
        
        
        
        brew install git
        brew update
        Now that your homebrew is up to date go ahead and install ruby:




        
        
        
        
        brew install ruby
        This will install ruby 1.9 on your system, and it will be neatly placed in /usr/local/Cellar/ruby directory. Rubygems are installed together with it but you want to update them before you continue (make sure you have set your PATH environment to begin with '/usr/local/bin' at this point, it is important). 
        Just run:




        
        
        
        
        gem update --system


          See, you don't even need to run it with sudo because you are using homebrew! Isn't it cool? Now that your gems are fresh, you can continue with rubyonrails.org instructions:




          
          
          
          
          gem install tzinfo builder memcache-client rack rack-test rack-mount erubis mail text-format thor bundler i18n


            However this is incomplete, you need to install rake and you probably want to have sqlite3 gem installed to, so go ahead and run this after first set of gems is installed:



            
            
            
            
            gem install sqlite3-ruby rake
            Who put "ruby" in the name of a gem? It baffles me! But let's continue. They way things are in the moment when I write this, rails 3.0 requires rack-mount to be version "0.4.0" so go ahead and install that version too, if the "install rails" fails for you.




            
            
            
            
            gem install rack-mount -v=0.4.0
            gem install rails --pre
            Now, because of homebrew specifics, this will not create a necessary symlink in /usr/local/bin for rails that are put in your ruby directory in Cellar, so let's do it ourselves:





            
            
            
            
            cd /usr/local && ln -s ../Cellar/ruby/1.9.1-p378/bin/rails bin/rails
            By the time you read this the version of your ruby can be different, so just run




            
            
            
            
            brew info ruby
            To see what version is it and where it is. If you want to be cool you can even do something like this:




            
            
            
            
            cd /usr/local && ln -s ../Cellar/ruby/`brew info ruby|grep "ruby "| awk '{print $2}'`/bin/rails bin/rails
            And it will work for any current version (as long as it is only one installed). At this point you are all set to go. Do your regular routine, like:




            
            
            
            
            rails ~/myapp
            cd ~/myapp
            ./script/rails generate Post name:string
            rake db:migrate
            ./script/rails server

            EDIT: as of now (2010/02/17) rails 3.0 are no longer broken, so this should produce a working setup. Just run

            
            
            
            
            gem update

            if you followed this instructions before and it didn't work.

            Thursday, February 26, 2009

            renaming files on Mac OS X with ruby

            So, for example you have bunch of ".htm" files that you want to rename to ".html"

            You need to open your Terminal.app "cd" to the directory where you files are and run this command:
            ruby -e "Dir.glob('*.htm').each{|file| File.rename(file,file.gsub('.htm', '.html'))}"

            If you want it to work for a current and all recursive directories you need to make a simple change:
            ruby -e "Dir.glob('**/*.htm').each{|file| File.rename(file,file.gsub('.htm', '.html'))}"