Ruby Weekly News 26th December 2005 - 1st January 2006
Ruby Weekly News is a summary of the week’s activity on the
ruby-talk mailing list / the comp.lang.ruby newsgroup / Ruby forum,
brought to you by Tim S…
A short one this week!
[ Contribute to the next newsletter ]
Articles and Announcements
* ICFP Contest Dates Are Set ---------------------------- James Edward G. II said that the International Conference of Functional Programming have announced the dates for 2006's
"Ruby participation has been pretty small in the past, so it
great to see some solid Ruby entries this year. Ruby Q. will
break for the contest, to encourage others to enter and give me
to make my own entry."
* RRobots Tournament Results ---------------------------- Ruby Q. #59 set out the challenge of writing bots to play in an RRobots Tournament, held just after Christmas. The results are now out, with Jannis Harder's bot Ente besting 10 other entries to win the tournament. Jannis also wins a real robot: the Desktop R/C Mini-Rover from ThinkGeek.com. (Thanks to Simon Kröger and James Edward G. II.) * Forthcoming 2nd ed. of _The Ruby Way_ --------------------------------------- This thread began a couple of weeks ago, but we missed it
Hal F. announced the second edition of his book `The Ruby
“ink is dry on the contract” and it’s aimed to be ready for the
quarter of 2006.
There was much discussion about the current edition, what
should be covered in the second edition, and also in response to
question by Patrick H., “what would your idea table of
look like in an Advanced Ruby book?”
David A. Black's upcoming book "Ruby for Rails: Ruby techniques
Rails developers" was also noted. It is due April 2006.
Diego: “I was wondering if ActiveRecord support was available for
access without requiring Ruby on Rails?”
The answer is yes, “Active Record works very well on standard alone
scripts” (David Heinemeier H.).
As an alternative, George M. mentioned his Og library, which
fills the same role as ActiveRecord.
“… and from the substratum, it arises …”
Devin M.: “scoped_require provides an optional parameter to
Kernel#require to allow you to shove the created modules/classes into
sandboxy container module. I use it to prevent namespace collision
external library. It’s a hack. Heed the version number.”
It works by reading the .rb file being required, and evaling the
Eero S. vaguely recalled someone posting an alternative
implementation of this concept - probably thinking of Wrapped Modules
Austin Z. in May 2005.
The example given in that post:
module My; end
module Your; end
require_wrap ‘cgi’, My
require_wrap ‘cgi’, Your
puts My::CGI.escape(“hello, world”)
puts Your::CGI.escape(“hello, world”)
Austin’s implementation works by first calling Module.constants to
out all the top-level constants, then calling require, followed by
Module.constants again to find which constants have been added. It
`moves’ those constants under the desired namespace.
Both implementations cause problems in some cases. The most
(and intractible) one seems to be the situation when the require’d
includes code like the following:
require ‘foo’, :module => F
Should the hello method be added to Ruby’s standard String class, or
should a new class F::String be created? (Would your answer be
if the class was called Util, and had just happened to be defined by
Austin’s solution does the former, while Devin’s does the latter.
Arguably, the `correct’ solution is to create F::String, and say that
foo.rb should have written class ::String if it really wanted to add
method to the standard String class.
This will be no consolation to all the libraries that don’t use the
prefix. For example, set.rb in the core Ruby library says module
Enumerable, not module ::Enumerable.
Using Float For Currency
Hunter’s Lists was using Floats to represent monetary values, but
to print e.g. “9.76” instead of “9.756”.
The immediate answer to the question is to use sprintf (or printf)
amount = 9.756
rounded = sprintf("%.2f", amount) # == “9.76”
Several people pointed out though that you shouldn’t use Floats to
Mental: “There are a lot of really nasty subtle issues that will lose
money between the cracks.”
This applies to any programming language, not just Ruby. An example
by Malte M.: 0.2 - 0.05 - 0.15 will be approximately
2.77555756156289e-17, not zero.
Stephen W.: “Floating point numbers represent an extremely wide
of values - much wider than their integer counterparts. This is
through an exponent and mantissa. For this ability, they trade off
The Ruby solution for exact decimals is BigDecimal:
a = BigDecimal.new(“0.2”)
b = BigDecimal.new(“0.05”)
c = a / b
Operations with BigDecimal are always exact (but slower than with
and so it is safe to use with currencies and other situations where
loss of precision is unacceptable.
Idiom wanted: do-while
Adam S. asked how to write a “do-while” loop in Ruby, in order to
remove the repetition in the below code:
b = simulate(b,m)
b = simulate(b,m)
James Edward G. II suggested loop, break unless, as in
b = simulate(b,m)
break unless another_turn?(b,m)
Ruby does support the following, which is just like a “do-while” loop
other languages, however Matz recently said on the ruby-core mailing
“Don’t use it please. I’m regretting this feature, and I’d like to
it in the future if it’s possible.”
b = simulate(b,m)
end while another_turn?(b,m)
Ruby on the mobile
“Do anyone knows if there is a Ruby version for cellphones and other
mobile devices on the make?”-Marcelo Paniagua.
Treefrog asked someone in the Mobile Devices division of Motorola
this a while ago, but was told that only Java and BREW were currently
Gene T. noted that Python is making progress in this area, linking
Python for Mobile Devices page, which lists a number of ports
official Nokia support and downloads for “Series 60” phones. (The
release by Nokia was over a year ago.)
People have previously reported success in running Ruby on the Sharp
Zaurus PDA (which runs Linux) and on Windows CE.
Ncurses - how do you get mousemask working?
Richard L. couldn’t get the mousemask to work in the Ncurses
| A few other projects have the line with `Ncurses::mousemask…’ in
| - but it’s always commented out… like they couldn’t get it
Paul D. replied “They couldn’t”, and posted a one-liner patch to
Ruby’s ncurses library to fix a bug.
Hopefully the authors of the other projects find out about this.