Blogged by Ujihisa. Standard methods of programming and thoughts including Clojure, Vim, LLVM, Haskell, Ruby and Mathematics written by a Japanese programmer. github/ujihisa

Friday, February 5, 2010

How To Check If You Have The Command

For example, to assume you have the command svn and you don't have the command svn1:

which svn >&/dev/null
if [ $? == 0 ]; then
  echo You have svn
fi

which svn1 >&/dev/null
if [ $? == 1 ]; then
  echo You don\'t have svn1
fi

Wednesday, February 3, 2010

The Right Way of Running External Commands on Vim

Today I was struggled with one of the worst behavior of Vim script.

Try the following code on your Vim.

:!echo "#!"

Assuming you know that the command :! is to run the given command on your shell. This looks equivalent to

$ echo "#!"

on your terminal.

But Vim will show a cryptic error. That's because both # and ! are special letters for :! command.

  • ! is the command previously ran. This is for the pseudo command :!! which means that running the same command again.
  • # is the filename previously you edited.
  • Similar in %

They may be useful for using Vim, but they make Vim script programmers crazy.

Wednesday, January 6, 2010

Gmail In Other Fonts

I use gmail. Gmail supports changing the layout. Gmail doesn't support changing the font family of messages. The default font family is Arial, but today I preferred to use Georgia instead. I tried to fix the font family with JavaScript, but it was totally tough.

My first code:

  • javascript: var a = document.getElementsByTagName('iframe'); for (var i in a) { var b = a[i].contentWindow.document.getElementsByTagName('div'); for (var j in b) { b[j].setAttribute && b[j].setAttribute('style', 'font-family: Georgia'); } }

It changes all texts in all <div>s, but the layout will be completely broken. The content of an email appears on the bottom of the browswer.

My second code:

  • javascript: var a = document.getElementsByTagName('iframe'); for (var i in a) { var b = a[i].contentWindow.document.getElementsByClassName('ii gt'); b.item() && b.item().setAttribute('style', 'font-family: Georgia'); }

It certainly changes the font of the content of an email, but when you moved to other messages, the font will back to the default ones.

Finally, my friend Hamachiya2 succeeded in writing the code of it.

  • javascript:(function(){ function cssr(doc, sel, dec) { var sheets=doc.styleSheets; if (sheets.length) { var tSheet=sheets[sheets.length-1]; tSheet.insertRule(sel+"{"+dec+"}",tSheet.cssRules.length); } }; var ifr=document.getElementById('canvas_frame'); cssr(ifr.contentWindow.document, '.gs', 'font-family: Georgia'); })()

Bookmarklet is here: ujihisa. Drag it to your bookmark bar.

To extend it for ease,

(function(){
  function cssr(doc, sel, dec) {
    var sheets=doc.styleSheets;
    if (sheets.length) {
      var tSheet=sheets[sheets.length-1];
      tSheet.insertRule(sel+"{"+dec+"}",tSheet.cssRules.length);
    }
  };
  var ifr=document.getElementById('canvas_frame');
  cssr(ifr.contentWindow.document, '.gs', 'font-family: Georgia');
})()

This works perfectly. Use it and bless Hamachiya2!

before

before

after

after

Sunday, January 3, 2010

Friday, January 1, 2010

Rabbit On Mac OS X

To use rabbit the presentation software written in Ruby, it is necessary to install gtk2.

$ sudo port install gtk2

will never be finished. I gave up the installation and typed <C-c>...

Wednesday, December 30, 2009

Trying Out Bundler In 1 Minute

Let's write an application which uses the gem library "g" without installing it globally.

Mise En Place

Install bundler to make the application easily.

gem install bundler

And if you already have g, uninstall it to make sure the application you'll make doesn't use the globally installed g.

gem uninstall g

Getting Started

$ mkdir ggg; cd ggg
$ vim Gemfile
gem 'g'
gem 'ruby-growl'

$ gem bundle
$ vim app.rb
require 'vendor/gems/environment'
require 'g'
g 'success!!!'

$ ruby app.rb

That's all!

For your information:

$ tree
.
|-- Gemfile
|-- app.rb
|-- bin
|   `-- growl
`-- vendor
    `-- gems
        |-- cache
        |   |-- g-1.3.0.gem
        |   `-- ruby-growl-1.0.1.gem
        |-- doc
        |-- environment.rb
        |-- gems
        |   |-- g-1.3.0
        |   |   |-- README.markdown
        |   |   |-- Rakefile
        |   |   |-- VERSION
        |   |   |-- g.gemspec
        |   |   |-- lib
        |   |   |   `-- g.rb
        |   |   `-- spec
        |   |       `-- g_spec.rb
        |   `-- ruby-growl-1.0.1
        |       |-- LICENSE
        |       |-- Manifest.txt
        |       |-- Rakefile
        |       |-- bin
        |       |   `-- growl
        |       |-- lib
        |       |   `-- ruby-growl.rb
        |       `-- test
        |           `-- test_ruby-growl.rb
        `-- specifications
            |-- g-1.3.0.gemspec
            `-- ruby-growl-1.0.1.gemspec

14 directories, 20 files

Monday, December 28, 2009

Usually Something, But If...

Which do you prefer to write in Ruby?

if boundary_condition
  code_for_the_extreme_case
else
  code_for_the_typical_case
  ...
end

Or

unless boundary_condition # `if !boundary_condition` as well.
  code_for_the_typical_case
  ...
else
  code_for_the_extreme_case
end

In such cases, first I try to use guards. This is straightforward.

return code_for_the_extreme_case if boundary_condition
code_for_the_typical_case
...

But sometimes I cannot use such notation in cases where not to use return or break.

Solution

I made a DSL for this problem.

class UsuallyPending
  instance_methods.map {|i| i.to_s }.
    reject {|i| /__/ =~ i }.
    each {|m| undef_method m }

  def initialize(b1)
    @b1 = b1
  end

  def but_if(cond, &b2)
    if cond
      b2.call
    else
      @b1.call
    end
  end
end

def usually(&b1)
  UsuallyPending.new(b1)
end

usually do
  p ARGV
  p 'hello!'
end.but_if ARGV.empty? do
  p 'Give me arguments!'
end

This is a straightforward expansion of postpositive if with block instead of a value.

Followers