Search By Google
Komentar
    Kalender
    Nopember 2009
    S S R K J S M
    « Feb    
     1
    2345678
    9101112131415
    16171819202122
    23242526272829
    30  
    Ngobrol Yuks
    • Seperti Orba, Polri Makin Tak Populer di Masyarakat 20 Nopember 2009
      Partai Gerakan Indonesia Raya (Gerindra) menyayangkan pemanggilan 2 media massa oleh Mabes Polri. Partai berlambang kepala burung garuda ini menilai hal ini persis seperti di zaman orde baru.
    • Ambil Lemak Tubuh untuk Kosmetik, Geng Peru Bunuh Manusia 20 Nopember 2009
      Geng di Peru ini sungguh keterlaluan. Geng itu sudah membunuh lusinan manusia hanya untuk diambil lemak tubuhnya sebagai bahan baku kosmetik!
    • Para Gubernur Dukung Sikap SBY Terhadap Rekomendasi Tim 8 20 Nopember 2009
      Para Gubernur mendukung sikap Presiden SBY terhadap rekomendasi Tim 8. Apapun sikap Presiden, para gubernur percaya sikap itu adalah yang terbaik bagi bangsa.
    • 3 Organisasi Pers: Pemanggilan Tak Ada Dasar Hukum Kuat 20 Nopember 2009
      3 Organisasi media menilai tak ada dasar hukum kuat tentang pemanggilan dua suratkabar harian Seputar Indonesia (Sindo) dan Kompas oleh Mabes Polri. Pemanggilan tersebut juga dinilai bertendensi melanggar UU Pers.
    • Tak Ada Masalah Pidana, Pemanggilan Bentuk Represif 20 Nopember 2009
      Pemanggilan dua suratkabar harian Sindo dan Kompas oleh Mabes Polri dinilai sebagai bentuk represif kepada media saat ini. Tindakan Polri tersebut disayangkan karena tak ada masalah pidana yang dilakukan media dalam hal ini.
    • Reza Tembus Babak Final Swedish Idol 2009 20 Nopember 2009
      Reza, 28 th, asal Jakarta berhasil masuk 5 besar finalis Swedish Idol 2009. Penampilan terbaik Reza membawakan New York New York (Liza Minelli, 1977) meledakkan standing ovation.
    • Kalau SBY Tak Takut Salah, Buka Saja Semua 20 Nopember 2009
      Mantan Jamintel Syamsu Djalal mempertanyakan sikap Presiden SBY yang terkesan lambat dalam menyikapi kasus bailout Bank Century yang merugikan negara sebesar Rp 6,7 triliun. Jika tidak takut bersalah, SBY sebaiknya tak ragu-ragu membuka kasus bailout ini.
    • Aparat Hukum Kotor, SBY Jangan NATO 20 Nopember 2009
      Presiden SBY diminta untuk segera mengambil sikap tegas dan berani untuk menindaklanjuti rekomendasi Tim 8, membersihkan aparat hukum yang kotor. SBY diminta jangan terlalu banyak berwacana namun tanpa aksi nyata.
    • Menhut Cabut Izin Sementara Perluasan HTI PT RAPP 20 Nopember 2009
      Menhut Zulkifli Hasan akhirnya nencabut izin sementara perluasan HTI PT Riau Andalan Pulp and Paper (RAPP) di Riau. Pencabutan izin sementara itu disampaikan Menhut saat rapat kerja dengan Komisi IV DPR.
    • Keterangan Media Didokumentasikan di Berita Acara Interview 20 Nopember 2009
      Redaksi dua media, Harian Kompas dan Harian Seputar Indonesia (Sindo), telah dipanggil Polri. Redaksi dua media itu juga telah dimintai keterangan. Namun, keterangan mereka didokumentasikan di berita acara interview (BAI).
    Category
    Buy Your Wordpress E-commerce Theme. Buy Now



    Proactol







    UniqueHoodia



    PenisHealth



    MoreNiche



    SizeGenetics



    AppStore Charts

    Installing Aptana on Ubuntu 8.04

    Februari 24th, 2009

    Quick install guide for Aptana on Ubuntu 8.04.  Modeled after the Installing Aptana Studio on Debian guide found at the Aptana page for Installing Aptana on Linux.  The issue with the guide provided by Aptana (at least for Ubuntu) is that Ubuntu comes with FF 3 Beta 5 (as of the time of this writing) by default.  Aptana doesn’t play nicely with FF 3 Beta 5, and after what appears to be a successful install, spits back an “embedded browser not available” error.  Note: you don’t have to worry about uninstalling FF 3!

    1. Install Java (sudo apt-get install sun-java6-jre sun-java6-plugin sun-java6-fonts)
    2. Install Firefox 2 (sudo apt-get install firefox-2)
    3. Download and unzip aptana to /usr/local/aptana
    4. Save the following script as runAptana.sh to /usr/local/aptana
      #!/bin/bash
      export MOZILLA_FIVE_HOME=/usr/lib/firefox
      /usr/local/aptana/AptanaStudio
    5. Make runAptana.sh executable (sudo chmod a+x runAptana.sh)
    6. Create a desktop launcher for Aptana and point it at runAptana.sh
      • Right click on your desktop
      • Click on Create Launcher
      • Name: Aptana
      • Command: browse for runAptana.sh
    7. Open firefox-2 Profile Manager (sudo firefox-2 -ProfileManager)
    8. Create ff Profile “aptana” dedicated for Aptana debugging.
    9. Start ff with the aptana Profile: (sudo firefox-2 -P aptana)
    10. Download from here http://update.aptana.com/update/firebug.xpi and install the firebug extension, modified for Aptana debugging. Close Aptana Profile of firefox.
    11. Start Aptana.
      • Go to Run -> Debug…
      • click on the Web Browser within the left pane
      • Press the ‘New’ button.
      • Within the right pane: Name the new configuration “Firefox”
      • Fill within:
        • Browser executable: /usr/bin/firefox-2
        • Arguments: -P aptana -no-remote

    JAVA SCRIPT : Java Script example

    Januari 6th, 2009

    Disini ada beberapa contoh p

    Contoh java script code

    Paypal : How to add multiple items in one button

    Desember 9th, 2008


    Hi, I was wondering if this was possible with PayPal cart or if I need to write up some JavaScript etc.

    So, I know when you use the _cart command and _upload you get item_x and amount_x etc, but when i tried to use it with just _cart and no _upload I get some error stating i need to enter in an amount.

    Does PayPal simply only allow you to add one item per button click? I would just use the 3rd party cart one but I need to be able to update the quantity within the cart.

    Here is what I’m trying to do: I have four products, and in one ordering section for ease of use I decided to have 4 input boxes and then an add to cart button. I then have a PHP script that will process the amount they put in, apply the correct price information, generate a form and submit it to paypal via javascript. I also have other sections on the site that just have one button per entry box and thoose also need to  be updated within the same cart.

    I’m not afraid of doing advanced things in PHP and JavaScript, I am just new to the PayPal cart ( that my client wanted me to use ) and I just don’t know all of its little quriks.

    The only thing I can think of is have four submits of a page, but once you submit the form you go to paypal…

    here is some code:

    <form target="paypal" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
    
    <input type="image" src="https://www.sandbox.paypal.com/en_US/i/btn/x-click-but22.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
    
    <img alt="" border="0" src="https://www.sandbox.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
    
    <input type="hidden" name="add" value="1">
    
    <input type="hidden" name="cmd" value="_cart">
    
    <input type="hidden" name="business" value="seller_1186539196_biz@stevenheller.net">
    <input type="hidden" name="item_name_1" value="Set of Four"  />
    
    <input type="hidden" name="amount_1" value="24.00"  />
    
    <input type="hidden" name="item_name_2" value="Color Placemat"  />
    
    <input type="hidden" name="amount_2" value="8.00"  />
    
    <input type="hidden" name="quantity_2" value="3"  />
    
    <input type="hidden" name="item_name_3" value="Fundraiser Level II - $10/set"  />
    
    <input type="hidden" name="amount_3" value="10.00"  />
    
    <input type="hidden" name="quantity_3" value="68"  />
    
    <input type="hidden" name="item_name_4" value="Fundraiser Level II - 68 sets @ $10/set"  />
    
    <input type="hidden" name="amount_4" value="680.00"  />
    
    <input type="hidden" name="quantity_4" value="1"  />
    </form>

    Linuxer : Setting Mail Server di Ubuntu Linux

    Desember 8th, 2008

    Yang harus diperlukan dan diinstall pada sistem linux anda, untuk menginstall paket pada distro yang lain silakan merujuk pada distro yang anda pakai:

    1.

    MTA (mail transer Agent )

    Mail transfer agent program yang digunakan untuk menerima dan mengirim email dari server anda,, yang digunakan disini adalah Postfix

    #apt-get install postfix
    2.

    Mail Filtering

    Digunakan untuk memfilter email yang masuk ke Server Mail anda untuk mendeteksi Spam & Virus, namun pada pembahasan kali ini tidak dibahas .. untuk topik advance pembahasan ini akan dibahas :D .
    3.

    Mail Delivery Agent

    Agar us bisa mendownload email dari lokasi yang lain kita membutuhkan server IMAP or POP3 disini banyak sekali pilihannya namun pada tutorial ini akan digunakan Dovecot.

    #apt-get install dovecot-common dovecot-imapd dovecot-pop3d

    Beberapa pilihan untuk IMAP & POP3 :
    1.

    Dovecot
    2.

    Courier
    3.

    Cyrus
    4.

    Webmail Server

    Agar Mail Server anda dapat diakses melalui lingkungan Web base anda memerlukan Webmail server berikut pilihannya:

    #apt-get install squirrelmail
    1.

    Squirrelmail
    2.

    OpenWebmail

    Yang digunakam pada tutorial ini adalah Squirrelmail
    5.

    WebServer (Lamp = Linux , apache , mysql, php)

    Untuk menjalankan Webmail Server anda membutuhkan Lamp sudah terinstalasi dengan baik ,, pada tutorial ini tidak dibahas bagaimana menkonfigurasi Lamp cobaah merujuk ke manual2 yang lain.

    Setelah anda berhasil menginstall paket2 yang disebutkan diatas step selanjutnya adalah megkonfigurasi Mail Server anda ….

    *

    Konfigurasi Dovecot
    o

    Menentukan Prorokol IMAP & POP3

    Edit konfigurasi pada /etc/dovecot/dovecot.conf

    tambahkan konfigurasi berikut: protocols = pop3 pop3s imap imaps
    o

    Memilih Mailboxes

    Agar mail disimpan pada home direktory maka kita edit konfigurasi /etc/dovecot/dovecot.conf

    rubahlah :

    #mail_location = maildir:~/Maildir menjadi mail_location = maildir:~/Maildir (pagarnya dihilangkan)
    o

    Testing2 dovecot

    root@w3hol:/home/rachmat# telnet localhost pop3

    Trying 127.0.0.1…

    Connected to localhost.

    Escape character is ‘^]’.

    +OK Dovecot ready.
    *

    Konfigurasi Alias squeirrelmail pada apache :

    karena saya menggunakan apache2 maka edit file konfigurasi /etc/apache/apache2.conf

    jika anda menggunakan distro fedora file konfigurasi ada pada /etc/apche/httpd.conf

    tambahkan script berikut pada bagian alias :

    Alias /webmail “/usr/share/squirrelmail/”

    Options Indexes MultiViews

    AllowOverride None

    Order allow,deny

    Allow from all

    jika anda menggunakan distro fedora anda tidak perlu mengkonfigurasi konfigurasi ini karena anda bisa langsung mengaksesnya dari http://localhost/webmail
    *

    okey sekarang kita selesai konfigurasi

    mari kita restart semua service yang dibutuhkan

    #/etc/init.d/apache2 restart untu distro fedora #/etc/init.d/httpd restart

    # /etc/init.d/dovecot restart

    #/etc/init.d/postfix restart

    buka browser … untuk mengakses squirrelmail buka alamat http://localhost/webmail

    dan login menggunakan user dan pass sistem linux anda… anda bisa berkirim dan menerima email di jaringan anda…. menggunakan email client seperti thunderbird, evolution dan lain2.

    Ruby : Learning Ruby 3 [Why Learning Ruby]

    Desember 4th, 2008

    As stated earlier, since Ruby is an open source offering it is free. Many people have opted to purchase the license so that they can make it a cross platform language as per their needs. Ruby can easily recognize variable types all by itself. Due to this feature coders are not required to define variable types as we have to do in several other programming languages. Ruby’s dynamic typing saves a lot of time for programmers.

    Ruby has been provided to programmers with various class libraries which are bundled together. These class libraries start from the basic data types to advanced level thread and network programming. These class libraries have not been saturated yet and Ruby still is in the process of getting more libraries with time to make programming more simple and efficient.

    Ruby also comes with an effective garbage collector which avoids the problem of memory leaks to a great extent and takes care of the misuse and unnecessary occupancy of the memory.

    Ruby provides us with familiar syntaxes which are known to C++,Eiffel, Perl, and Python programmers. These syntaxes are composed of all the common features available for the programming like the comments, identifiers, reserved words, literals, arrays, Regular expressions etc.

    Ruby : Learning Ruby 2 [History of Ruby]

    Desember 3rd, 2008

    Ruby was introduced by Yukihiro Matsumoto (known widely as “Matz”) in 1993 and was officially released by Matz in 1995.

    Rails framework was designed and given by David Heinemeier Hansson in 1994. It was developed under the MIT License system and thus made Ruby on rails an open source and free to be implemented by everyone.

    This is one important reason why Ruby on Rails has acquired a significant position in programming.

    Ruby : Learning ruby 1 [What is Ruby]

    Desember 1st, 2008

    Ruby was introduced by Yukihiro Matsumoto (known widely as “Matz”) in 1993. Ruby is a dynamic interpreted language which has many strong features of various languages. It is a strong Object oriented programming language which also has single inheritance as in Java. It also provides us with the feature called as mixins. Using mixins we can easily import methods from multiple classes using modules.

    Ruby also has the scripting feature similar to the Python and Perl. The Object oriented concept from C++ and Java also maintains the reliability of programming in addition to maintaining the security of code.

    Ruby is open source which means that it is free to be used; one does not need to pay anything to use it. Because of this feature of Ruby it is used worldwide by everyone.

    JAVA : Call EJB3 from jetty

    Nopember 27th, 2008

    
    FYI I got the injection to work. When I was putting together a demonstration the injection started working. The problem has to do with dependencies in the pom.xml. I am not sure if there was something extra causing the problem or perhaps the order of the jars in the classpath. I am uploading an example

    By the way this example demonstrations embedding openEJB in Jetty. The important piece of the puzzle is in the WAR/pom.xml:


    <plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>maven-jetty-plugin</artifactId>
    <configuration>
    <scanIntervalSeconds>5</scanIntervalSeconds>
    <contextPath>/example</contextPath>
    <systemProperties>
    <systemProperty>
    <name>java.naming.factory.initial</name>
    <value>org.apache.openejb.client.LocalInitialContextFactory</value>
    </systemProperty>
    <systemProperty>
    <name>java.naming.factory.url.pkgs</name>
    <value>org.mortbay.naming</value>
    </systemProperty>
    </systemProperties>
    </configuration>
    <dependencies>
    </dependencies>
    </plugin>

    

    The system property “java.naming.factory.url.pkgs” is the key. From what I can tell openEJB’s IvmContext will attempt to do JNDI lookups and if it fails will look for additional ObjectFactories and delegate to them. By adding the “org.morbay.naming” system property IvmContext finds the Jetty JNDI context. Without this setting the jetty plugin will fail with the error: “javax.naming.NameNotFoundException: Name “java:comp” not found.”

    source : http://www.nabble.com/Re:-Tomcat—Injection:-No-such-property-p16421933.html

    Ruby : Tentang Ruby

    Nopember 24th, 2008

    Salah satu alasan bahasa scripting semakin populer adalah karena kesederhanaannya, powerful, dan sangat mudah digunakan serta dikembangkan. Bahasa scripting semakin diakui keberadaannya, salah satu buktinya adalah Google dan Nokia yang menggunakan bahasa scripting Python dalam produk mereka.

    Mungkin Anda selama ini sudah cukup puas dengan bahasa pemrograman yang ada seperti Java, C, C++, VB, dan sebagainya.

    Mengapa kita memerlukan bahasa scripting?
    Bahasa scripting dirancang untuk tipe aplikasi yang berbeda dengan bahasa pemrograman
    sistem seperti C dan C++. Bahasa scripting sangat cocok digunakan sebagai glue language(bahasa perekat) misalnya untuk membuat interface antarkomponenkomponen.

    Walaupun bahasa scripting bersifat interpreter dan lebih lambat dibandingkan dengan bahasa compiler seperti C dan C++, hal tersebut jarang diungkit sebab bagian penting dari suatu program misalnya yang berhubungan dengan sistem tetap akan dibuat dengan bahasa pemrograman sistem seperti C dan C++, sedangkan bahasa scripting berfungsi sebagai glue
    language untuk menghubungkan antar komponen–komponen sistem tersebut misalnya untuk GUI interface.

    Pada umumnya, bahasa scripting memiliki string processing yang sangat akurat misalnya regular expression sehingga sangat cocok untuk administrator sistem untuk membuat shell script yang powerful. Di pihak lain, bahasa scripting menawarkan pengembangan aplikasi yang cepat misalnya pembuatan aplikasi GUI, web scripts, system utilities, dan aplikasi yang membutuhkan pemrosesan string ataupun perhitungan yang akurat.

    Dalam perhitungan, bahasa scripting tidak kalah jika dibandingkan dengan bahasa compiler seperti C dan C++.

    Bahasa Ruby lahir pada tanggal 23 Februari 1993 dan masuk ke Amrik pada tahun 2000. Pada awalnya, pembuat bahasa ini Yukihiro “Matz” Matsumoto semasa kuliahnya sangat menyenangi pemrograman berorientasi objek dan bahasa scripting, kemudian dia melakukan riset terhadap bahasa scripting Perl dan Python.

    Akan tetapi dia tidak menemukan yang diinginkannya. Menurutnya bahasa Perl kurang powerful dan bahasa Python kurang object-oriented, sehingga ia memutuskan untuk
    membuat suatu bahasa yang lebih powerful daripada Perl dan lebih object-oriented dari Python. Lahirlah bahasa baru yang dinamakan Ruby yang berarti sejenis batu permata.

    Dewasa ini, Ruby sudah mulai diterima oleh para programmer dunia. Konon, Ruby lebih
    populer daripada Python di Jepang.

    • Ruby merupakan bahasa interpreter.
    • Ruby memiliki sintaks yang sederhana, mudah dipelajari dan dipahami.
    • Ruby mendukung exception handling seperti halnya Java dan Python.
    • Ruby murni merupakan bahasa berorientasi objek di mana semua datanya adalah objek, begitu pula dengan angka 8 yang merupakan instansi dari kelas FixNum.
    • Ruby mendukung single-inheritance dan modul mix-in yang serupa dengan interface di Java.(Multiple-inheritance dapat digantikan dengan menggunakan fitur mix-in ini).
    • Ruby memiliki portabilitas yang tinggi antar-platform sehingga dapat berjalan di berbagai OS seperti UNIX, Linux, DOS, Windows 95/98/Me/NT/2000/XP, MacOS, BeOS, OS/2, dan sebagainya.
    • Ruby mendukung dynamic-typing, seperti halnya Python di mana kita tidak perlu mendeklarasikan tipe untuk suatu variabel.
    • Ruby mendukung garbage collection seperti halnya Java dan Python di mana Anda tidak perlu membebaskan memory yang dialokasi( mis. free() di C) . Variabel yang tidak lagi digunakan akan segera dibebaskan oleh garbage collector. Dengan demikian, Anda tidak perlu berhubungan dengan manajemen memory yang merumitkan.
    • Ruby mudah dikembangkan dengan bahasa C seperti halnya Python misalnya dengan menggunakan interface SWIG.
    • Ruby lahir dari komunitas, sehingga Ruby memiliki dukungan komunitas yang siap membantu Anda jika menemui kesulitan.
    • Dan yang terakhir, tentunya Ruby gratis bahkan untuk aplikasi komersial.

    NGOPAS source : Dasar Pemrograman Ruby

    Ruby : Ruby Basic Tutorial

    Nopember 24th, 2008


    Ruby can be used as a fully object oriented language, in which case you’d create classes and objects to accomplish everything. However, it can be used quite nicely with only the objects and classes that ship with Ruby, in which case it can be used as a procedural language, except that functions are typically methods of the program’s variables.

    If that doesn’t make any sense to you, don’t worry, it’s just a way of saying that Ruby can be very easy to learn and use.

    Even if you want to become a Ruby expert, you need to learn the basic functionality before you can become a Ruby OOP ninja. This tutorial gives you those basics.

    Hello World
    This is the simplest possible Ruby program, hello.rb. As you’d expect, it prints “Hello World” on the screen. Be sure to set it executable.

    #!/usr/bin/ruby
    print “Hello World\n”

    Although this program works as expected, it goes against the philosophy of Ruby because it’s not object oriented. But as a proof of concept that Ruby’s working on your computer, it’s just fine.

    Besides print, there’s also a puts keyword. The difference is that puts automatically inserts a newline at the end of the string being printed, whereas print does not. In other words, puts is more convenient, but print is necessary if separate statements print to the same line. Througout this tutorial we’ll use both print and puts.
    Loops
    Let’s count to 10…

    #!/usr/bin/ruby
    for ss in 1…10
    print ss, ” Hello\n”;
    end

    The elipses (…) indicate the range through which to loop. The for is terminated by an end. You don’t need braces for a loop. Whew!

    The following is the output:

    [slitt@mydesk slitt]$ ./loop.rb
    1 hello
    2 hello
    3 hello
    4 hello
    5 hello
    6 hello
    7 hello
    8 hello
    9 hello
    [slitt@mydesk slitt]$

    Notice that it stops on 9. The number following the elipses causes termination at the top of the loop. The 1…10 means 1 TO BUT NOT INCLUDING 10, it does NOT mean 1 through 10. Please remember this when using Ruby loops.

    NOTE

    There are actually two versions of the elipses operator, the three period version as shown previously, and the two period version. The two period version is inclusive. In other words, 1…3 means 1 up to but not including 3, while 1..3 means one through 3.

    By using the appropriate version of the elipses operator you can save having to code convoluted end conditions.

    Now let’s iterate through an array.

    #!/usr/bin/ruby
    presidents = ["Ford", "Carter", "Reagan", "Bush1", "Clinton", "Bush2"]
    for ss in 0…presidents.length
    print ss, “: “, presidents[ss], “\n”;
    end

    We defined an array of presidents using a Perl like syntax (except we used brackets instead of parens), and we iterated from 0 (Ruby is 0 based, like most languages), through the final subscript in the presidents array. Remember, the triple dot stops before executing the final number, which is why it doesn’t count to 6. If you had wanted it to count to 6 (which in this case would have walked off the end of the array), you would have used the double dot. The output of the preceding code follows:

    [slitt@mydesk slitt]$ ./loop.rb
    0: Ford
    1: Carter
    2: Reagan
    3: Bush1
    4: Clinton
    5: Bush2
    [slitt@mydesk slitt]$

    Now lets iterate backwards through the array, using the fact that array[-1] is the last item, array[-2] is the second to last, etc:

    #!/usr/bin/ruby
    presidents = ["Ford", "Carter", "Reagan", "Bush1", "Clinton", "Bush2"]
    for ss in 0…presidents.length
    print ss, “: “, presidents[presidents.length - ss - 1], “\n”;
    end

    The preceding program produces the following output:

    [slitt@mydesk slitt]$ ./hello.rb
    0: Bush2
    1: Clinton
    2: Bush1
    3: Reagan
    4: Carter
    5: Ford
    [slitt@mydesk slitt]$

    Of course, the preceding was a very contrived example just to demonstrate negative subscripts. Note that the subscripts no longer match the presidents, which is probably not what you want. You probably want to do it like you’d do it in any language — tweak the subscript to the end value and decrease it:

    #!/usr/bin/ruby
    presidents = ["Ford", "Carter", "Reagan", "Bush1", "Clinton", "Bush2"]
    for ss in 0…presidents.length
    ss_tweaked = presidents.length - ss - 1
    print ss_tweaked, “: “, presidents[ss_tweaked], “\n”
    end

    If you’re familiar with C, Pascal or Perl, you’re probably dissappointed you couldn’t just use presidents.length…0. Backwards iteration doesn’t work in Ruby — it must iterate up.

    Iterators and Blocks
    Another way to loop through an array is to use an iterator (in red in the following code) and a block (in blue in the following code:

    #!/usr/bin/ruby
    presidents = ["Ford", "Carter", "Reagan", "Bush1", "Clinton", "Bush2"]
    presidents.each {|prez| puts prez}

    In the preceding code, the block argument (prez) contains the current array element, and everything else until the closing brace contains code to operate on the block argument. The block argument is always enclosed in vertical lines (pipe symbols). The following is the output of the preceding code:

    [slitt@mydesk slitt]$ ./hello.rb
    Ford
    Carter
    Reagan
    Bush1
    Clinton
    Bush2
    [slitt@mydesk slitt]$

    The block needn’t be on one line:

    #!/usr/bin/ruby
    presidents = ["Ford", "Carter", "Reagan", "Bush1", "Clinton", "Bush2"]
    presidents.each {
    |prez|
    puts prez
    }

    As shown in the previous examples, you can define the block by enclosing it in curly braces. You can also define it by enclosing it in a do and an end, where the do replaces the opening brace, and the end replaces the closing brace:

    #!/usr/bin/ruby
    presidents = ["Ford", "Carter", "Reagan", "Bush1", "Clinton", "Bush2"]
    presidents.each do
    |prez|
    puts prez
    end

    Personally, I greatly prefer the do/end syntax for multiline blocks, because as a Perl/C/C++ guy I have a very different perception of braces than their limited use in Ruby, and also because of all the brace placement religious wars I’ve endured (I’m a Whitesmith type guy myself). However, on short single line blocks, using the braces saves valuable line space. From what I understand, the methods are interchangeable in features and performance, with one small exception…

    Speaking of performance, if you declare the block argument outside the block (in other words, make it a local variable), performance improves because Ruby needn’t recreate a variable every iteration. HOWEVER, the loop messes with the value of the variable, so it’s best to use a specific variable for that purpose, and do not use it for other purposes within the subroutine. Here’s an example of using a local variable as a block argument:

    #!/usr/bin/ruby
    i = -99
    puts “Before: ” + i.to_s
    (1..10).each{|i| puts i}
    puts “After : ” + i.to_s

    [slitt@mydesk slitt]$ ./loop.rb
    Before: -99
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    After : 10
    [slitt@mydesk slitt]$

    If you use a local variable for a block argument, do so only in loops with huge numbers of iterations, and use only variables that are specifically intended to serve as block arguuments and nothing else.
    A Difference Between {} and do/end
    As mentioned, there’s one small difference between brace enclosed blocks and do/end enclosed blocks: Braces bind tighter. Watch this:

    #!/usr/bin/ruby
    my_array = ["alpha", "beta", "gamma"]
    puts my_array.collect {
    |word|
    word.capitalize
    }
    puts “======================”
    puts my_array.collect do
    |word|
    word.capitalize
    end

    [slitt@mydesk slitt]$ ./test.rb
    Alpha
    Beta
    Gamma
    ======================
    alpha
    beta
    gamma
    [slitt@mydesk slitt]$

    The braces bound tightly like this:

    puts (my_array.collect {|word| word.capitalize})

    Whereas do/end bind more loosely, like this:

    puts (my_array.collect) do |word| word.capitalize} end

    Note that the latter represents a syntax error anyway, and I’ve found no way to coerce do/end into doing the right thing simply by using parentheses. However, by assigning the iterator’s results to a new array, that array can be used. It’s one more variable and one more line of code. If the code is short, use braces. If it’s long, the added overhead is so small a percentage that it’s no big deal:

    #!/usr/bin/ruby
    my_array = ["alpha", "beta", "gamma"]
    puts my_array.collect {
    |word|
    word.capitalize
    }
    puts “======================”
    new_array = my_array.collect do
    |word|
    word.capitalize
    end
    puts new_array

    [slitt@mydesk slitt]$ ./test.rb
    Alpha
    Beta
    Gamma
    ======================
    Alpha
    Beta
    Gamma
    [slitt@mydesk slitt]$

    Generally speaking, if you want to directly use the result of iterators, use braces. For longer blocks, do/end is more readable, and the overhead for the extra variable and line of code is trivial.
    while Loops
    All the loops previously discussed looped through either an array or a set of numbers. Sometimes you need a more generic loop. That’s when you use a while loop:

    #!/usr/bin/ruby
    ss = 4
    while ss > 0
    puts ss
    ss -= 1
    end
    puts “======================”
    while ss < 5
    puts ss
    ss += 1
    break if ss > 2
    end
    puts “======================”
    ss = 5
    while ss > 0
    puts ss
    ss -= 2
    if ss == 1
    ss += 5
    end
    end

    [slitt@mydesk slitt]$ ./loop.rb
    4
    3
    2
    1
    ======================
    0
    1
    2
    ======================
    5
    3
    6
    4
    2
    [slitt@mydesk slitt]$

    The first while loop iterated from 4 down to 1, quitting when ss became 0 and hit the while condition. The second loop was intended to iterate up to 4 and quit when 5 was encountered, but a break statement inside the loop caused it to terminate after printing 2 and then incrementing to 3. This demonstrates the break statement.

    The third loop was intended to loop from 5 down to 1, quitting after printing 1 and then decrementing. However, the statement in the body of the loop added 5 when it reached 1, pushing it back up to 6, so it had to count down again. On the second countdown, the numbers were even, so it didn’t trigger the if statement. This shows that unlike Pascal, it’s OK to tamper with the loop variable inside the loop.

    Read the rest of this entry »