» beginings of ruby by do_guh_new |
|
(Login to remove green text ads)
explanation of the language and a simple example
There are many things to like about this little used language. (at least over here in japan it's been a widly popular since the mid '90's) Ruby is simple and concise with a simple syntax you can get a lot done with a remarkably short program. It's totally object-oriented; everything is an object or can be made into an object. And of course the first rule of writing about languages is that the first example must be a "Hello, World!" program, so here it is:
Code:
puts "Hello, World. Time: #{Time.now}"
explanation:
Ruby has a built in method called puts(). It writes its argument to standard output and appends a newline if there isnt one already. The cool thing is the #{Time.new} stuff. If Ruby finds the construct #{expr} in double quoted strings, it evaluates the enclosed expression then converts the result to a string and subsitutes that back into the original. The expression can be anything from a simple variable to a sizable block of code. In the above case the expression is a call to the now() method of the class Time, which returns a new Time object initialized to the current time. When converted to a string, time objects look comfortingly familiar, so if we ran this code, the puts() method would send the string to standard output and we would see output like the following:
Code:
Hello, World. Time: Sat Jun 15 24:24:54 EST 2002
|
|