Eiffel for Rubyists
After several months programming with Ruby scripting language, and related libraries, I got quite used to the intensive use of iterators and blocks (closures) that give Ruby programs a particular coding flavour. Switching back to Eiffel, I wondered how to reproduce this coding style in Eiffel programs: apart from discussing about this being an healthy practice or not, let's see some funny examples !
Simple iterators
One of the most frequently used iterators in Ruby is the each method in container classes like Array:
languages = Array.new(['Eiffel', 'Ruby', 'Python', 'C++', 'Perl', 'Java']) languages.each do |lang| puts "I like " + lang end
this fragment of code basically gives:
I like Eiffel
I like Ruby
I like Python
I like C++
I like Perl
I like Java
The same result can be achieved in Eiffel by means of the do_all iterator of ARRAY class; the basic step is to substitute Ruby blocks (the portion do |varbind| ... end) with Eiffel inline agents:
Integer class iterators
Other common Ruby iterators can be found in the Integer class:
4.times do |t| puts "Hello, world!" end 1.upto(5) do |n| puts "going up: #{n}" end 10.downto(5) do |n| puts "going down: #{n}" end
The output of these three instructions is the following:
Hello, world!
Hello, world!
Hello, world!
Hello, world!
going up: 1
going up: 2
going up: 3
going up: 4
going up: 5
going down: 10
going down: 9
going down: 8
going down: 7
going down: 6
going down: 5
In Eiffel, INTEGER class is not equipped with such iterators, one could think of an INTEGER_ITERATOR class that implements them or, alternatively, write the following features that make the first integer argument explicit:
Keeping in mind the Ruby example, these features should be called as follows:
Reading a file the Ruby way
And now a more advanced example, in Ruby a text file can be read as follows:
File.open("myfile.rb", "r") do |infile| while (line = infile.gets) puts line end end
The open method in class File takes as argument a block operating on the file itself, it also takes care of closing the file after block execution, indeed a neat and compact style (but maybe not so explicit: should it be called open_do_something_and_close ?).
So, let's mimic this behaviour in Eiffel:
class RUBY_FILE exports the open feature as seen for the File class in Ruby, an usage example is the following:
have fun with Eiffel and Ruby !
integer intervals and file line processing
upto can be done in Eiffel with integer intervals:
it doesn't currently allow descending but we're not too far...
For reading files I'd rather see an iterator like API rather, where the processing agent takes a line, than having to iterate lines by hand: solves 2 problems in one go.
closure...
Although generally somewhat more verbose, some patterns known from more functional languages can sometimes lead to very elegant solutions in Eiffel as well.
What I found a bit disappointing is that an inline agent can not capture local variables of the function it is being defined in though. (or am I missing something? Is this intended behaviour?)
You can by simply provide the value. This was a conscious decision from the ECMA specification no to allow reuse of the parent locals because the semantic could become complex (e.g. what happens if the parent scope is gone?). The solution is to write your code as:
Same as call agents
You aren't missing anything.
Inline agents are functionally identical to call agents, but they ensure that your code is less readable, and less maintainable.
Colin Adams
Reference Guide
Hi, Where can I See the full reference guide for eiffel? Thanks!
Most of the EiffelStudio documentation is at http://docs.eiffel.com and it includes a language description. Or you can go to the ECMA website and fetch the PDF of the Eiffel standard.
The only reference guide found on this page was this one: http://docs.eiffel.com/book/method/quick-reference-eiffel-language Which contains nothing.
Is there any online full docs, like the Java Docs? I'm trying to learn eiffel, and this wold be very helpful
Thanks!
See pages from http://docs.eiffel.com/book/method/quick-reference-eiffel-programming-language