Reading gem version from YAML
From Jekyll:
1 module YerGem 2 def self.version 3 yml = YAML.load(File.read(File.join(File.dirname(__FILE__), *%w[.. VERSION.yml]))) 4 "#{yml[:major]}.#{yml[:minor]}.#{yml[:patch]}" 5 end 6 end
How to fix issues with missing gem specifications
I was getting this error after unpacking hpricot with gem unpack hpricot. I also tried rake gems:unpack hpricot but it did nothing…
1 config.gem: Unpacked gem hpricot-0.8.1 in vendor/gems has no specification file. Run 'rake gems:refresh_specs' to fix this.
The rake gems:refresh_specs command doesn’t work, and appears to have been a temporary workaround, so to fix this error I did this:
1 cd vendor/gems/hpricot-0.8.1 2 gem specification hpricot > .specification
I had this issue with Rails 2.3.4.
How to profile your Rails and Ruby applications with ruby-prof
Installing ruby-prof
First install ruby-prof:
1 git clone git://github.com/jeremy/ruby-prof.git 2 cd ruby-prof/ 3 rake gem 4 sudo gem install pkg/ruby-prof-0.6.1.gem
Note that version 0.6.0 doesn’t work, at least not with Rails 2.1.1. With 0.6.0 I got this message:
1 `gem install ruby-prof` to use the profiler
Setting up a new environment for profiling
Create config/environments/profiling.rb:
1 config.cache_classes = true 2 config.action_controller.consider_all_requests_local = false 3 config.action_controller.perform_caching = true 4 config.action_view.cache_template_loading = true 5 6 #config.log_level = :debug
Add the new environment to database.yml. You might want to reuse the development database.
Creating a profiling script
Next we’ll create a script that simply fetches the homepage, save the following code in profiling/homepage.rb:
1 get '/' 2 say "GET / => #{path}"
Run the script
Now run the script 100 times:
1 RAILS_ENV=profiling ./script/performance/request -n 100 profiling/homepage.rb
Profiling plain Ruby applications
You can also profile a block of code by calling RubyProf from your code:
1 require 'ruby-prof' 2 3 # Profile the code 4 RubyProf.start 5 ... 6 [code to profile] 7 ... 8 results = RubyProf.stop 9 10 File.open "#{RAILS_ROOT}/tmp/profile-graph.html", 'w' do |file| 11 RubyProf::GraphHtmlPrinter.new(results).print(file) 12 end 13 14 File.open "#{RAILS_ROOT}/tmp/profile-flat.txt", 'w' do |file| 15 RubyProf::FlatPrinter.new(results).print(file) 16 end 17 18 File.open "#{RAILS_ROOT}/tmp/profile-tree.prof", 'w' do |file| 19 RubyProf::CallTreePrinter.new(results).print(file) 20 end
Analyzing results
I prefer to use the RubyProf::CallTreePrinter to output data that kcachegrind can read. The HTML and text data is difficult to read so kcachegrind will definitely make your life easier.
On OSX you can install kcachegrind with Fink (or DarwinPorts):
1 sudo apt-get update ; sudo apt-get install fink 2 sudo apt-get install kcachegrind
There’s also WinCacheGrind and MacCacheGrind, but I haven’t tried those.
How to install the stemmer4r gem on Mac OS X and Linux
The stemmer4r gem is fubar. Warning draft snippet…
1 # gem install stemmer4r 2 Bulk updating Gem source index for: http://gems.rubyforge.org 3 Building native extensions. This could take a while... 4 ERROR: While executing gem ... (Gem::Installer::ExtensionBuildError) 5 ERROR: Failed to build gem native extension. 6 7 ruby extconf.rb install stemmer4r 8 9 Gem files will remain installed in /usr/lib/ruby/gems/1.8/gems/stemmer4r-0.6 for inspection. 10 Results logged to /usr/lib/ruby/gems/1.8/gems/stemmer4r-0.6/ext/stemmer4r/gem_make.out 11 12 13 1. Change path of Ruby executable 14 15 cd /usr/lib/ruby/gems/1.8/gems/stemmer4r-0.6/ext/stemmer4r/ 16 vim extconf.rb 17 18 #!/usr/bin/ruby -w 19 20 to 21 22 #ruby -w 23 24 2. Compile libstemmer_c 25 26 cd /usr/lib/ruby/gems/1.8/gems/stemmer4r-0.6/ext/stemmer4r/libstemmer/ 27 make 28 29 3. Compile stemmer4r 30 31 cd /usr/lib/ruby/gems/1.8/gems/stemmer4r-0.6/ext/stemmer4r/ 32 33 Change path: 34 /usr/local/ruby/lib/ruby/1.8/i686-linux/ 35 To: 36 /usr/lib/ruby/1.8/x86_64-linux/ 37 38 Or wherever you have it installed 39 40 ruby extconf.rb 41 42 43 4. Build stemmer4r gem 44 45 46 gem build stemmer4r.gemspec 47 48 gem install stemmer4r-0.6.gem 49 50 51 Problems 52 53 gcc -shared -rdynamic -Wl,-export-dynamic -L"/usr/lib" -o stemmer4r.so stemmer4r.o libstemmer_c/libstemmer.o -lruby1.8 -lpthread -ldl -lcrypt -lm -lc 54 /usr/bin/ld: libstemmer_c/libstemmer.o(libstemmer.o): relocation R_X86_64_32 against `a local symbol' can not be used when making a shared object; recompile with -fPIC 55 libstemmer_c/libstemmer.o: could not read symbols: Bad value 56 collect2: ld returned 1 exit status 57 make: *** [stemmer4r.so] Error 1 58 59 60 Add CFLAGS: 61 62 root@aktagon:/usr/lib/ruby/gems/1.8/gems/stemmer4r-0.6/ext/stemmer4r/libstemmer_c# make 63 include mkinc.mak 64 CFLAGS = -fPIC 65 libstemmer.o: $(snowball_sources:.c=.o) 66 $(AR) -cru $@ $^ 67
Error when installing Mongrel from gem
I received the following error when installing Mongrel from the gem repository:
1 marko@x61s:$ sudo gem install mongrel 2 Updating metadata for 281 gems from http://gems.rubyforge.org 3 complete 4 Building native extensions. This could take a while... 5 ERROR: Error installing mongrel: 6 ERROR: Failed to build gem native extension. 7 8 /usr/bin/ruby1.8 extconf.rb install mongrel 9 extconf.rb:1:in `require': no such file to load -- mkmf (LoadError) 10 from extconf.rb:1
The fix is to install the ruby development package:
1 sudo apt-get install ruby1.8-dev