Really compact Ruby quick reference guide

Hello World in Ruby

General Rules
Comments start with # and ends with newline
Newlines or semicolon can be used separate expressions
Everything in Ruby is an object. This includes constants such as 5
Since even operators are methods, they can be overridden
Elegant code can be written using blocks feature

Ruby Keywords

alias   and     BEGIN   begin   break   case    class   def     defined
do      else    elsif   END     end     ensure  false   for     if
in      module  next    nil     not     or      redo    rescue  retry
return  self    super   then    true    undef   unless  until   when
while   yield

Ruby Types
Basic types are numbers, strings, ranges, regexen, symbols, arrays, and hashes.

Symbols
:symbol
Unique and comparable values that can be substituted for string keys.

Ranges
1..10 – includes last value (10)
1…10 – excludes last value (10)

Arrays
var = Array.new
var = [10,20]
var = %w (string1,string2)

Hashes
var = Hash.new
var = {1=>2, 2=>3, 3=>4}

Variable Types
$global_variables
@@class_variables
@instance_variables
CONSTANT (caps)
local_variables

Dummy Variables
self – current method’s object
nil
true,false
__FILE__
__LINE__

Predefined Variables

$!         The exception message set by raise call.
$@         Array of backtrace of the last exception thrown.
$&         The string matched by the last successful pattern match.
$`         The string to the left  of the last successful match.
$'         The string to the right of the last successful match.
$+         The last bracket matched by the last successful match.
$1         The Nth group of the last successful match. May be > 1.
$~         The information about the last match.
$=         The flag for case insensitive, nil by default.
$/         The input record separator, newline by default.
$         The output record separator for the print and IO#write.
$,         The output field separator for the print and Array#join.
$;         The default separator for String#split.
$.         The current input line number of the last file that was read.
$<         The virtual concatenation file of the files from command line.
$>         The default output for print, printf. $stdout by default.
$_         The last input line of string by gets or readline.
$0         Contains the name of the script being executed.
$*         Command line arguments given for the script sans args.
$$         The process number of the Ruby running this script.
$?         The status of the last executed child process.
$:         Load path for scripts and binary modules by load or require.
$"         The array contains the module names loaded by require.
$DEBUG     The status of the -d switch.
$FILENAME  Current input file from $<. Same as $<.filename.
$LOAD_PATH The alias to the $:.
$stderr    The current standard error output.
$stdin     The current standard input.
$stdout    The current standard output.
$VERBOSE   The verbose flag, which is set by the -v switch.

Operator Precedence (highest to lowest)
:: .
[]
**
-(unary) +(unary) ! ~
* / %
+ -
<< >>
&
| ^
> >= < <=
<=> == === != =~ !~
&&
||
.. …
=(+=, -=…)
not
and or

Ruby Control Structures

Method Invocation and Definition
method_call
object.method_call
Class::method_call

def method_name
method_body
end

Blocks
method_invocation do … end
method_invocation {

}

Exception Handling (2 forms)

Classes

Accessor Shortcuts
attr_reader
attr_writer
attr

Ruby IDE – Ruby development tools for Eclipse

RDT is an open source IDE for Ruby implemented as an eclipse plugin. So if you are familiar with eclipse platform, this is the best way to start Ruby development. RDT is written in Java along with some Ruby scripts to interface with Ruby code being written.

This tools is far from perfect. It features syntax highlighting, code completion and code formatting. It also provides a basic Test::Unit integration. RDT also comes with a simple regular expression tester.

RDT requires eclipse 3 or above version. You can download Ruby Development Tools here.

RubyUnit – A simple testing framework for Ruby

RubyUnit is a simple Ruby testing framework developed by Masaki Suketa. Like Junit, RubyUnit requires your test cases to extend from RUNIT::TestCase. The license under which it is release is same as that of Ruby.

You can download RubyUnit from here.

Following is a sample RubyUnit test case code,

3 reasons why you must learn ruby language

Ruby ProgrammingThere are dozens of programming languages out there. For a beginner, typically the first language these days are Java, C++ or C#. Each programming language has its own strengths and weaknesses. In fact, Ruby language implementation is still way behind some of the powerful Java virtual machines. But still I feel Ruby is a must learn language for a programmer. Here is why,

1. Ruby is the most elegant language – Ruby is the most elegant language I have come across.  It is also the most flexible language. You can redefine almost anything and can add to builtin classes. In fact class definitions can be extended on the fly!

2. Ruby makes you ultra productive – Ruby makes you ultra productive. There is minimum plumbing code required and you can focus on the programming problem. A beautiful feature called blocks (closure) can make your code efficient and intuitive.

3. Ruby is on the growth phase – Ruby has been out there for sometime. But lately it is getting a lot of attention mainly due to the ruby on rails Web application framework. Ruby on rails lets you build instant Web applications with minimum effort.  If you are a programmer, your next project may be on Ruby!

Ruby on Rails and Java?

Guys at the RailsEnvy.com site has created a video comparing Ruby on Rails with Java. Actually they are comparing J2EE with ROR. It seems to suggest that to develop any Web application, you need everything from Hibernate to Spring.

But reality is slightly different. None of the technologies are essential. You can create industry strength Java Web application by using Spring DAO. Hibernate is not a must for it!

I do agree that being a recent framework, Ruby on Rails is an improvement in many respects over other frameworks. At the same time it may not be that safe to use it on a heavy duty site as the twitter debacle had shown.

Anyway I am not envious of Ruby or Rails. In fact I don’t understand the friction between Ruby and other languages. My advice is this, if you feel threatened by Ruby, learn it. :-) At the same time I don’t understand why some Ruby developers are so arrogant. I guess they have no practical experience in other languages!

First post on Ruby Tips

Welcome to Ruby Tips. This Website is dedicated to the most beautiful programming language on earth – Ruby Language. Like any programming book, I will start this site by writing a “Hello World” program in Ruby.

Can it be any simpler?

Ruby is a reflective, dynamic, object-oriented programming language. Ruby programs are intuitive, yet highly compact. Many call it as ”programmer’s best friend”.