Ruby 1.9 – Tap my Object

posted by Arkham on October 28th, 2011

Let’ say you need to debug this piece of code:

very_cool_method(egg, sausage)

What would you do? I guess something along the lines of:

spam = very_cool_method(egg, sausage)
p spam

Well, Ruby 1.9 let you do something cooler instead:

very_cool_method(egg, sausage).tap{ |s| p s }

The cool thing about tap is that you can use it to eavesdrop method chains:

very_cool_method(egg, sausage).tap{ |s| p s }.now_make_sandwich_from(bacon)

Hooray! :)

Tags: , , , ,

Another 17 Vim Tips

posted by Arkham on October 6th, 2009

success_vim

  1. <Ctrl>-k + e + ! – Write ‘è’ character.
  2. <Ctrl>-k + e + – Write ‘é’ character.
  3. !!date – Insert date in current line.
  4. g, / g; – Jump back/forward to the locations that have been edited.
  5. zf – Fold selected text.
  6. zfa} – Fold the function enclosing the cursor.
  7. zR / zM – If foldmethod is set, open/close all folds.
  8. <Ctrl>-w + (/+) – In splitted views, reduce/increase current zone height.
  9. <Ctrl>-w + (</>) – In splitted views, reduce/increase current zone width.
  10. :vimgrep /pattern/ files – Search for pattern in the provided list of files.
  11. :cn and :cN to jump between vimgrep results.
  12. :clist to display all vimgrep result and :cc number to jump to a specific one.
  13. :%s/\<and\>/or/g – Replace all occurences of “and” with “or”, leaving words like “demand” unchanged.
  14. :%s/foo/bar/gi – Replace case insensitively.
  15. * / # – Search for the next/previous occurence of the word under the cursor.
  16. /\cfoo\Cbar/ – Matches “foobar”, “Foobar” and “FOObar”.
  17. /\([a-z]\)\([a-z]\)\2\1/ – Matches “abba” and “deed”.

Tags: , , , ,

Sed by examples, Part 2

posted by Arkham on June 29th, 2009

sed_logo_white

sed (stream editor) is a Unix utility which (a) parses text files and (b) implements a programming language which can apply textual transformations to such files.
It reads input files line by line (sequentially), applying the operation which has been specified via the command line (or a sed script), and then outputs the line.

Substitution

  1. Substitute, for every line, the first occurence of foo (if any) with spam:

    # test.txt
    foo bar spam foo
    spam foo bar egg
    sausage foo spam egg
    spam spam spam
    sed -e 's/foo/spam/' test.txt
  2. Substitute every occurence of foo with spam:

    sed -e 's/foo/spam/g' test.txt
  3. Substitute every occurence of foo with spam in the first two lines:

    sed -e '1,2s/foo/spam/g' test.txt
  4. Substitute every occurence of foo with spam in every line that starts with sausage and ends with egg:

    sed -e '/^sausage.*egg$/s/foo/spam/g' test.txt
  5. Substitute paths:

    sed -e 's|/usr/bin/|/usr/local/bin|g' script.sh
  6. Remove html tags:

    <html>
    <body>
    Hello World!
    </body>
    </html>
    sed -e 's/<[^>]*>//g' hello.html

Advanced substitution

  1. Append (that’s what she said) to every line ( & represents what matches):

    sed -e "s/^.*$/& (that's what she said)/g" test.txt
  2. Append lol, rofl and lmao to the first three words of every line:

    sed -e 's/\(^[^ ]*\) \([^ ]*\) \([^ ]*\)/\1lol \2rofl \3lmao/' test.txt

Multiple Commands

  1. Print a file alternating row numbers:

    sed -n -e '=;p' test.txt
  2. Print some information about your cpu first core:

    # script.sed
    1,/^$/{
        /model name/p
        /flags/p
        /bogomips/p
    }
    sed -n -f script.sed /proc/cpuinfo
  3. Add a line before/after each line or replace line:

    # script.sed
    i\
    This line will be inserted before each line
    # script.sed
    a\
    This line will be inserted after each line
    # script.sed
    c\
    This line will be inserted in each line
    sed -f script.sed test.txt
  4. Tags: , , , ,

Sed by examples

posted by Arkham on June 29th, 2009

Canon Tosh SED

sed (stream editor) is a Unix utility which (a) parses text files and (b) implements a programming language which can apply textual transformations to such files.
It reads input files line by line (sequentially), applying the operation which has been specified via the command line (or a sed script), and then outputs the line.

Delete

  1. Delete the first line of a file:

    sed -e '1d' /etc/fstab
  2. Delete from the second to the tenth line:

    sed -e '2,10d' /etc/fstab
  3. Delete lines starting with #:

    sed -e '/^#/d' /etc/fstab

Print

  1. Print lines starting with #:

    sed -n -e '/^#/p' /etc/fstab
  2. Print blocks of text enclosed by BEGIN and END:

    sed -n -e '/BEGIN/,/END/p' script.awk
  3. Print Device section in xorg.conf:

    sed -n -e '/Section "Device"/,/EndSection/p' /etc/X11/xorg.conf
  4. Print main function in a C source file:

    sed -n -e '/main[[:space:]]*(/,/^}/p' source.c

Tags: , , , ,

Ukulele Lessons from Uke Minutes

posted by Arkham on May 29th, 2009

22913

uke-minute-1-accessories
uke-minute-2-basic-strum
uke-minutes-3-ukulele-sizes
uke-minutes-4-diy-ukulele-case-humidifier
uke-minutes-5-how-to-re-string-your-ukulele
uke-minutes-6-the-roll-technique
uke-minutes-7-history-of-the-ukulele
uke-minutes-8-major-scales
uke-minutes-9-major-chords
uke-minutes-10-hammer-ons

uke-minutes-11-in-to-out-picking
uke-minutes-12-free-strokes-rest-strokes
uke-minutes-13-tremolo-technique
uke-minutes-14-minor-scales-chords
uke-minute-15-5-finger-roll
uke-minutes-16-finger-stretching-warm-ups
uke-minutes-17-movable-chord-shapes
uke-minutes-18-chord-families
uke-minutes-19-pull-offs-and-hammer-pull-combos
uke-minutes-20-pinky-mute

uke-minutes-21-tapping
uke-minutes-22-pluck-strum-with-jake-shimabukuro
uke-minutes-23-chunking
uke-minutes-24-two-finger-pulloffs
uke-minutes-25-7th-chord-shapes
uke-minutes-26-strum-picking
uke-minutes-27-major-7th-shapes
uke-minutes-28-scale-exercise-i
uke-minutes-29-scale-exercise-ii
uke-minutes-30-how-to-read-tabs

uke-minutes-31-harmony-3rds
uke-minutes-32-basic-tab-notation
uke-minutes-33-how-to-tune-your-ukulele
uke-minutes-34-buying-an-ukulele
uke-minutes-35-c-string-bass-technique
uke-minutes-36-3-finger-chunk
uke-minutes-37-one-handed-harmonics
uke-minutes-38-artificial-harmonics
uke-minutes-39-left-hand-exercise-i
uke-minutes-40-first-beat-c-string

uke-minutes-41-muted-octave-strum
uke-minutes-42-left-hand-exercise-ii
uke-minutes-43-suspended-chords
uke-minutes-44-hammer-on-chords

Tags: , , ,