Ruby Language

Ruby is a DynamicallyTyped, ObjectOriented AgileLanguage, which came to popularity mainly thanks to RubyOnRails web framework. Ruby was specifically intended as an improvement over PerlLanguage, but its closest cousin is PythonLanguage. After initial rivalry, it came to be understood that Ruby and Python have complementary design goals. Ruby and Python developers are now supporting each other.

Useful pages:
Hello world in Ruby:'
 print "Hello World!\n"
or, using the more common Ruby idiom:
 puts "Hello World!"
or, for CeePlusPlus fans:
 $stdout << "Hello World!\n";
or, for CeeLanguage fans:
 printf("Hello World!\n");
or, for those who like their output raw:
 p "Hello World!"

Articles: [1] This article is fairly old now, and many of the complaints it has about Python no longer apply.

HelpMe: I need a reliable language which lives in ordinary files and has good up-to-date libraries and (most importantly) good up-to-date web frameworks. Obviously RubyLanguage is the way to go. But having used IoLanguage a lot, and also played with SmalltalkLanguage, LispLanguage and IokeLanguage, RubyLanguage just seems BORING. Can anyone help me get really excited about it please? -- JasonGrossman

It's like with sex - language is just a tool to achieve the goal. Ruby is top-quality, but you don't want to get too excited about machine tools. Sure, Makino Seiki tool grinder is cool, but do you need it? Can it pay for itself? Do you own a workshop? Do you have a product? Customers? Ruby is comfortable, but don't believe anyone who says it is easy. Achieving full command of its power takes months for geniuses, years for us ordinary people. Unless you are solving a very difficult problem, stick with those languages you already know, or PythonLanguage, which is catching up with Ruby comfort with every new release. It is not the machine beauty, but the problem you are solving that should excite you. If it doesn't, your gut might be telling you that you are trying to do something else than you really want. Search your soul and find a more attractive goal (not necessarily in the realm of computers), and you will love the tools that help you to achieve it. BorisStitnicky

Not sure I get the comparison with sex, but I think Boris's last three sentences are a profound observation into programming and life. -- DavidMeyer


Praise for Ruby:


For me, Ruby is the successful combination of SmallTalk's conceptual elegance, Python's ease of use and learning, and Perl's pragmatism.


I like it too! It has some kind of elegance, like EiffelLanguage, but it has a Perlish flavor nevertheless... I've found the developers and those on ruby-talk pretty supportive and friendly...

One thing I absolutely love: Procs (i.e. code blocks)... -- DavidSdeLis? (David also wants you to look at BrainLanguage, http://brain.sourceforge.net.)


With depth of its OO support, Ruby is close to Smalltalk. In Ruby, everything is an executable statement, and up to a small number of exceptions, these are simply method calls. For example, to declare a class with a (read-only) attribute, you could write:
	class Example
	  attr :fred
	end
The difference is, that while language syntax is not user-modifiable, methods can all be redefined by the user, including built-in classes. The 'attr' line might look like syntax, but is actually a method call to a Class object named Example (yup, Ruby has metaclasses). The user can redefine 'attr' like this:
	class Class
	  def attr; puts "attr has been redefined!" end
	end
If you want to, you could redefine #attr to trace all reads or writes to the variable, or perhaps to implement some kind of lazy persistence... Same goes for operators:

  1. + 3 == 5 || 3 + 4 == 8 # result is, obviously, false

is actually understood as

  1. +( 3 ).==( 5 ).||( 3.+( 4 ).==( 8 ) )

where #+, #==, #|| are just method names. Add to this depth facilities such as iterators, closures, mixins, and a clean syntax, and you end up with a language that's practical for building scalable projects, but also fun to play with.


I played with Ruby for a short time and I like it very much. It seems to combine likeable qualities of Perl, Python, and Smalltalk (except for the use of begin/end rather than {/} to delimit block, except procs, where either do/end and {/} is allowed. The Python philosophy is a little too much "There's only one way to do it" for my taste. Ruby combines the cleanliness of Python with the flexibility and concision of Perl and the pure object-orientedness of Smalltalk. -- DanSchmidt


I've written scripts in Perl, Python, and Ruby. I like Ruby best by far. Now if I can get my ISP to put it up on my web site without killing me (after I pushed him for Perl and Python) ... -- RonJeffries



Request for SUCCESS real file stories for ruby.... waiting for the miracle... profit... challenge... success... -- AndreySidorenko

One area where Ruby really shines is in creating domain specific languages. Turns out that Ruby's blocks make this easy to do. I created a Hardware Description Language/simulation environment using Ruby (RHDL) that ended up looking very much like an existing HDL (VHDL). No parser was needed - it's all pure Ruby. This would have been very difficult to do in Python since Python doesn't seem to have anything equivalent to Ruby's blocks. BTW: I was able to implement the first version of RHDL in about 300 lines of Ruby. In addition to Ruby's blocks, the fact that Ruby has continuations also helped a lot. -- PhilTomson?


Ruby requires a lot less boilerplate than many other languages. Commands don't need to terminate with a semicolon; they simply end at endline. (Or the continue to the nextline if you implicitly state that the command's not over, using e.g. a trailing "+", or explicitly using a trailing "\".) Variable names aren't preceded with a $, method call parenthesis can be dropped...

Isn't it easier to make a dumb syntactical mistake, without those restrictions?

Sure it is, but you get used to the quirks soon enough. For more discipline, use PythonLanguage.


Hedging about Ruby:


Maybe I am just not willing to get too bleeding edge, but after looking at the Ruby site (URL at top of this page), I chose not to spend time learning Ruby at this time. I have been experimenting with Python, and have used it for some text manipulation projects with great success. One example is parsing an SNMP MIB and creating a header file with the corresponding MIB variables in it. That way, as Marketing changes the MIB around, I just run a script to recreate the header file. I only have to change my Cpp code if they add or remove variables. Perl seems much more widely used than Python, but Python's readability convinced me to use it. Ruby may be a better language than Python or Perl, but too few people are using it, and the documentation is still too obviously translated from Japanese for me to feel comfortable using it. -- AnonymousDonor



I'll preface this by stating that I actually love Ruby. It's a great scripting language for my needs, and I've been using it quite a bit... enough that I start running into the warty, awkward bits that plague any language. My two "favourites":

  $ ruby script.rb
  script.rb:<last line of file>: syntax error
I swear to God, Ruby has the worst diagnostics of any program I've ever used except ed. It's like the old joke about "KenThompson's automobile"... 90% of the time, it won't say anything more useful than "syntax error", and usually it can't even give you the correct line number to start looking at. Oy gevalt!

Phew, that was cathartic.


I've noticed the syntax error at end of file syndrome as well. It usually seems to mean that you started a construct but missed it's corresponding end. I've been reduced to commenting out swathes of code (say, an entire class) in an attempt to track this kind of thing down. Let's say it's an area of the parser which could be improved. And while I'm on the subject, a couple of my pet peeves:

  10.times
  {
	|i|
	puts i
  }



Speaking of refactoring - where is the RefactoringBrowser for Ruby? I've recently been getting into learning it, but I realized how spoiled I was with the EclipseIde's support for Java refactorings. -- StevenNewton

''A RefactoringBrowser for Ruby has been written: http://www.kmc.gr.jp/proj/rrb/index-en.html. It works with EmacsEditor and FreeRIDE. -- pate


While Ruby is nice, I can't help but think that the designer has a bit too much of the "OOP is the light and the way" philosophy in him...

Ruby is not dogmatic about ObjectOrientedProgramming. Ruby is very scalable language and you can pick the appropriate style for each problem. Strictly speaking, Ruby doesn't have stand-alone functions. As for not having stand-alone functions, try passing a block into Proc.new and the object that you get back is close enough for any use that I can think of. -- BenTilly


Oh, yeah. And = assigns, while == compares. Duh!

One day I'd like to see a language that uses := for assignment, == for comparison, and leaves = as a syntax error. EeLanguage.

My own experience has been that rigid language doesn't prevent me from making mistakes. -- francis

How about : for assignment and = for comparison, like the SelfLanguage? That's one character for each, and I never ever mix them up.

How about making variables objects? ...

Ruby variable symbols are preceded by 0-2 at-signs depending on their scope. Local variable is called simply last_word, instance variable @last_word, and class variable @@last_word. I don't know if Matz intended it it this way, but this makes it easier to see dependencies at a glance.

Have a look at http://relativity.yi.org/el/code-keywords.el. (BrokenLink 2005-05-01) It colours words like TODO and FIXME. You may be able to modify it to colour single and double at-signs. -- ChanningWalton


Software implemented in Ruby:


TestingFrameworks


User stories


As a long time Perl user, I love the power of Perl, but have been dissatisfied with the cryptic nature of Perl code. In particular, using objects in Perl is painful. I've been meaning to make the move to Python just to get better OO support, but I was finding it difficult to translate my Perl based knowledge into Python. I discovered Ruby this past summer and found it easy to make the switch from Perl to Ruby. All the power of Perl, with a clean syntax and an OO system that is very reminiscent of Smalltalk. I highly recommend looking into Ruby. -- JimWeirich


In a bake-off between Java, Python and Ruby for my new Web site project, we selected Ruby and I'm very happy. I love the language syntax - it is short and readable at the same time, matches my intuitive thinking mode even better than Smalltalk (I evaluate a language on that and shortnes of typing [millimemes/keystroke is the unit of measure, but it has to stay readable]. ... That having been said, I detest the @ and @@ variable names as they really clutter up the eye and make the rest of the code harder to read, and the absence of a development environment is <unspeakable>. Programming back in the 1980s, (shudder). ... So you might take it as strong evidence of the niceness of the language that we are using it anyway. -- AlistairCockburn (See TheProblemWithSigils about @ and @@)


In summary, this language makes you feel loved. (In remarkable contrast to some other languages.) -- PhlIp


I personally like to use Ruby too but I've found some quirks that bother me slightly. A few weeks back I was deciding whether to use Python or Ruby and I read both tutorials. In the end (after a long self-debate) I chose for Ruby as it's cleaner, though it lacks the richness in libraries of Python.

Recently, however, I've noticed the quirk I was mentioning: when you access an instance-variable with an attr_reader, you can modify it. (Description of user's encounter with passing by reference followed here.) Now I know that Ruby uses pass-reference-by-value (Like Java) but still, I'd expect an attr_reader to give a cloned object... Just my 2 cents -- ChristophePoucet? P.S: I still use Ruby happily

	class Class
	  def attr_clone *args
		args.each { |arg|
		class_exec { define_method arg do send( arg ).clone end }
		}
	  end
	end

# Later use this newly defined cloning accessor: class Foo attr_clone :myvar end

This really helps cut down on code size a lot. -- EivindEklund

I have used both Ruby and Squeak (SmalltalkLanguage implementation) and I must say that while Ruby is more pragmatic, Squeak is more interesting. The whole environment understands objects, the browser is absolutely fantastic, etc. I think if there was a Squeak-like environment for Ruby, it would be great. Although Ruby is pure OO, Smalltalk is even more. I like that ifTrue:, ifFalse: and ifTrue:ifFalse: are methods of the boolean class.


I don't see people complaining about ugly Perlisms in Ruby. Though I quite like Ruby, I felt since the beginning that a lot of fluff needs to be thrown out. All of those $-initial variables (especially $_, aaargh), and the statement modifiers.


"Come to think of it, does Ruby have higher order functions?"

Ruby has a convention for higher order function emulation, yes. Sadly, it's not unified with the block mechanism, which aims at partly the same solutions. BTW: Can you write anonymous "functions" like these (objects with a call method) in Ruby? (Answer: Yes) And less related, can an iterator (or whatever a method receiving a block is called) pass the block to a subroutine? (Answer: Yes)


Final remarks and references


See InspectEvalFileFormat, BlocksInRuby

Bow before RubyForge.

Learn to use RubyGems.

Use Bundler to create your own gems.
Ruby interfaces to other programming tools


RubyGui

There are several ways to form a Ruby GUI. Try eg. RuguiGem?.
Interprocess Communication using Ruby


Logging from Ruby


Embed Ruby in a web page


Ruby Books


Ruby Patterns


CategoryProgrammingLanguage CategoryRuby CategoryScripting

EditText of this page (last edited December 7, 2014) or FindPage with title or text search