Read first:

- vim and ruby scripts step by step

- introduction to regular expressions

- wget: download files from the command line

vim load_mediawiki_list.rb #Or use your favorite editor and create the file load_mediawiki_list.rb

<a> # pulse the “a” key (omit if you are using other editor)


$stdin.read.each{|line| puts "#{$1} => #{$2}" if line =~ /<li>([^:]*): (.*)/}

:wq # save and close in Vim

wget “http://wiki.favpal.org/Seekr_City/Abbreviations” -O - | ruby load_mediawiki_list.rb

Wget is a program for downloading files or webpages from the command line. Of course, it is available in many GNU/Linux distributions and operating systems.

We are going to use it a lot and three of the most common uses are the next ones:

wget http://www.gutenberg.org/dirs/etext95/study10.txt

The file is downloaded as saved as study10.txt We could define the file name with the option “-O”:

wget http://www.gutenberg.org/dirs/etext95/study10.txt -O book.txt

Or you also could send it to the standard output with “-O -” and use a pipe (|):

wget http://www.gutenberg.org/dirs/etext95/study10.txt -O - | grep Holmes

After the introduction to regular expressions, we are going to play a bit with a Sherlock Holmes book by Conan Doyle (free download on gutenberg.org):

Open a terminal and:


wget http://www.gutenberg.org/dirs/etext95/study10.txt

irb

book=IO.read("study10.txt")

i = book=~/Watson/
=> 15078
book[i-50..i+10]
=> "m.\r\n\r\n\"Whatever have you been doing with yourself, Watson?\"\r\n"

i = book=~/\sWatson\s/
=> 15078
book[i-50..i+10]
=> "click\r\nof the latch as she opened it.\r\n\r\n\"Does Dr. Watson liv"

i = book=~/(\w*) Street/
=> Baker
book[i-50..i+10]
=> "his\r\nrooms with me.  \"I have my eye on a suite in Baker Stree"

a=[]
book.each{|line| a<
	<line if line=~/(\w*) Street/}
=> ...

a.size
=> 9

puts a
=> rooms with me.  "I have my eye on a suite in <strong>Baker Street</strong>,"
at No. 221B, {5} <strong>Baker Street</strong>, of which he had spoken at our
and we stood together at the corner of <strong>Henrietta Street</strong> a-talkin'.
...

“Regular expressions provide a concise and flexible means for identifying strings of text of interest, such as particular characters, words, or patterns of characters.

The following examples illustrate a few specifications that could be expressed in a regular expression:

* the sequence of characters “car” in any context, such as “car”, “cartoon”, or “bicarbonate”
* the word “car” when it appears as an isolated word
* the word “car” when preceded by the word “blue” or “red”
* a dollar sign immediately followed by one or more digits, and then optionally a period and exactly two more digits

Regular expressions can be much more complex than these examples.” (from Wikipedia)

Now, open a terminal, introduce “irb” or go to tryruby and continue playing:


1. "My child loves cartoons and dogs"=~/car/
=> 14

# \s: whitespace
"My child loves cartoons and dogs"=~/[\s]car[\s]/
=> nil

"My child loves cars and dogs"=~/[\s]cars[\s]/
=> 14

"My child loves cars! and dogs"=~/[\s]cars[\s]/
=> nil

# \w: any word character
# [^\w]: any character except word characters
"My child loves cars!"=~/[^\w]cars[^\w]/
=> 14

"My child loves cars"=~/[^\w]cars[^\w]/
=> nil

#$ end of the string
"My child loves cars"=~/[^\w]cars$/
=> 14

#([^\w]|$) any non word character or end of the string
"My child loves cars"=~/[^\w]cars([^\w]|$)/
=> 14

"My child loves blue cars"=~/blue cars/
=> 15

"My child loves red cars"=~/(blue|red) cars/

=> 15
$1
=> red

"My child loves blue cars"=~/(blue|red) cars/
=> 15
$1
=> blue

More info here:

YAML

June 20, 2008


require 'yaml'
=> true

any_object=[2,3,"hi"]
=> [2, 3, "hi"]

dumped=YAML.dump(any_object) # You could store it in a file
=> "--- \n- 2\n- 3\n- hi\n"

YAML.load(dumped)
=> [2, 3, "hi"]

irb

File.open("file.txt","w"){|f| f.puts "override with this"}
=> nil
IO.read("file.txt")
=> "override with this\n"

File.open("file.txt","w"){|f| f.puts "override with this 2"}
=> nil
IO.read("file.txt")
=> "override with this 2\n"

File.open("file.txt","a"){|f| f.puts "add this"}
=> nil
text=IO.read("file.txt")
=> "override with this 2\nadd this\n"

puts text
> override with this
> add this
=> nil

irb
system(”ls”)
> …

“string #{2*5}”
> “string 10″

exit

vim each_args.rb
<a>


$*.each{|n| puts n}

<Esc>
:wq

ruby each_args.rb argument1 argument2
> argument1
> argument2

In Ubuntu, install the convert package:
sudo aptitude install convert

vim reduce_image.rb
<a>


$*.each{|file| system("convert #{file} -resize 50% #{file}")}

<Esc>
:wq


wget http://upload.wikimedia.org/wikipedia/commons/9/97/The_Earth_seen_from_Apollo_17.jpg -O Earth.jpg
wget http://upload.wikimedia.org/wikipedia/commons/d/dd/Full_Moon_Luc_Viatour.jpg -O Moon.jpg

ls -l *jpg
> -rw-r–r– 1 hector hector 6511067 2005-07-31 04:25 Earth.jpg
> -rw-r–r– 1 hector hector  924214 2006-10-07 23:59 Moon.jpg

ruby reduce_image.rb *jpg
ls -l *jpg
> -rw-r–r– 1 hector hector 2521991 2008-06-14 23:14 Earth.jpg
> -rw-r–r– 1 hector hector  246682 2008-06-14 23:14 Moon.jpg

ruby reduce_image.rb *jpg
ls -l *jpg
> -rw-r–r– 1 hector hector 689873 2008-06-14 23:15 Earth.jpg
> -rw-r–r– 1 hector hector  71721 2008-06-14 23:15 Moon.jpg

Vim is a really useful text editor. Its based on commands interface may frighten you but don't worry, it's easy to start playing and you'll start to see its power soon.

In Ubuntu, install the vim-full package:
sudo aptitude install vim-full

Download it here for Windows or see this for other operation systems.

You also could install gvim which is a graphical interface for vim:
sudo aptitude install gvim

And just start the play:

vim args.rb # (or gvim args.rb if you want the graphical interface)
Pulse the a key (<a> afterwards)
$*.each{|n| puts n}
<Esc>
:wq

ruby args.rb arg1 arg2 arg3
>arg1
>arg2
>arg3

vim upcase.rb
Pulse the a key (<a> afterwards)
puts $*[0].upcase
<Esc>
:wq

ruby upcase.rb "hello"
=>HELLO

echo "hello" | ruby upcase.rb
=>

vim upcase.rb
<a>
<use the arrows for moving the cursor>
arg0_or_stdin = $* == [] ? $stdin.read : $*[0]
puts arg0_or_stdin.upcase
<Esc>
:wq

echo "hello" | ruby upcase.rb
=>HELLO

Conditionals in Ruby

June 16, 2008

Open a terminal and play with the if and unless clauses:

irb

a=Time.now.month
=> 6

"June" if a==6
=> "June"

"not January" unless a==1
=> nil

if Time.now.year==2008
  out="ok"
else
  out="really?"
end
=> "ok"

# And other shorter way:
out = Time.now.year==2008 ? "ok" : "really?"
=> "ok"

Note: Ruby on Rails is available for other GNU/Linux distributions, Mac OS and Windows so you don’t have to use Ubuntu, but you could give it a try and download it from here (or even request an Ubuntu CD for free).

Open a terminal and follow this great article (but keep in mind that the Slicehost articles are for servers):

sudo aptitude install ruby1.8-dev ruby1.8 ri1.8 rdoc1.8 irb1.8 libreadline-ruby1.8 libruby1.8 libopenssl-ruby sqlite3 libsqlite3-ruby1.8

sudo ln -s /usr/bin/ruby1.8 /usr/bin/ruby

sudo ln -s /usr/bin/ri1.8 /usr/bin/ri

sudo ln -s /usr/bin/rdoc1.8 /usr/bin/rdoc

sudo ln -s /usr/bin/irb1.8 /usr/bin/irb

mkdir ~/sources

cd ~/sources

wget http://rubyforge.org/frs/download.php/34638/rubygems-1.1.0.tgz

tar xzvf rubygems-1.1.0.tgz

cd  rubygems-1.1.0

sudo ruby setup.rb

sudo ln -s /usr/bin/gem1.8 /usr/bin/gem

sudo ln -s /usr/bin/gem1.8 /usr/bin/gem

sudo gem update --system

sudo gem install rails

sudo aptitude install postfix subversion