26th December 2005 - 1st January 2006

http://www.rubyweeklynews.org/20060101.html

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 

programming
contest.

   "Ruby participation has been pretty small in the past, so it 

would be
great to see some solid Ruby entries this year. Ruby Q. will
take a
break for the contest, to encourage others to enter and give me
time
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 

previously.

   Hal F. announced the second edition of his book `The Ruby 

Way’;
“ink is dry on the contract” and it’s aimed to be ready for the
second
quarter of 2006.

   There was much discussion about the current edition, what 

libraries
should be covered in the second edition, and also in response to
a
question by Patrick H., “what would your idea table of
contents
look like in an Advanced Ruby book?”

   David A. Black's upcoming book "Ruby for Rails: Ruby techniques 

for
Rails developers" was also noted. It is due April 2006.

Threads

ActiveRecord

Diego: “I was wondering if ActiveRecord support was available for
database
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.

scoped_require 0.0

“… 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
some
sandboxy container module. I use it to prevent namespace collision
with an
external library. It’s a hack. Heed the version number.”

It works by reading the .rb file being required, and evaling the
result.

Eero S. vaguely recalled someone posting an alternative
implementation of this concept - probably thinking of Wrapped Modules
by
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
find
out all the top-level constants, then calling require, followed by
Module.constants again to find which constants have been added. It
then
`moves’ those constants under the desired namespace.

Both implementations cause problems in some cases. The most
significant
(and intractible) one seems to be the situation when the require’d
file
includes code like the following:

foo.rb

class String
def hello
“hello #{self}”
end
end

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
different
if the class was called Util, and had just happened to be defined by
another library?)

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
the
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
wanted
to print e.g. “9.76” instead of “9.756”.

The immediate answer to the question is to use sprintf (or printf)
for
rounding:

amount = 9.756
rounded = sprintf(“%.2f”, amount) # == “9.76”

Several people pointed out though that you shouldn’t use Floats to
represent currency.

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
given
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
range
of values - much wider than their integer counterparts. This is
handled
through an exponent and mantissa. For this ability, they trade off
precision.”

The Ruby solution for exact decimals is BigDecimal:

require ‘bigdecimal’

a = BigDecimal.new(“0.2”)
b = BigDecimal.new(“0.05”)

c = a / b

Operations with BigDecimal are always exact (but slower than with
Float),
and so it is safe to use with currencies and other situations where
any
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)
while another_turn?(b,m)
b = simulate(b,m)
end

James Edward G. II suggested loop, break unless, as in

loop do
b = simulate(b,m)
break unless another_turn?(b,m)
end

Ruby does support the following, which is just like a “do-while” loop
in
other languages, however Matz recently said on the ruby-core mailing
list
“Don’t use it please. I’m regretting this feature, and I’d like to
remove
it in the future if it’s possible.”

begin
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
about
this a while ago, but was told that only Java and BREW were currently
supported.

Gene T. noted that Python is making progress in this area, linking
to a
Python for Mobile Devices page, which lists a number of ports
including
official Nokia support and downloads for “Series 60” phones. (The
first
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
library.

| A few other projects have the line with `Ncurses::mousemask…’ in
them
| - but it’s always commented out… like they couldn’t get it
working
| either.

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.

[Now with correct thread name. Was missing the "Ruby Weekly News "
prefix.]

http://www.rubyweeklynews.org/20060101.html

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 

programming
contest.

   "Ruby participation has been pretty small in the past, so it 

would be
great to see some solid Ruby entries this year. Ruby Q. will
take a
break for the contest, to encourage others to enter and give me
time
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 

previously.

   Hal F. announced the second edition of his book `The Ruby 

Way’;
“ink is dry on the contract” and it’s aimed to be ready for the
second
quarter of 2006.

   There was much discussion about the current edition, what 

libraries
should be covered in the second edition, and also in response to
a
question by Patrick H., “what would your idea table of
contents
look like in an Advanced Ruby book?”

   David A. Black's upcoming book "Ruby for Rails: Ruby techniques 

for
Rails developers" was also noted. It is due April 2006.

Threads

ActiveRecord

Diego: “I was wondering if ActiveRecord support was available for
database
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.

scoped_require 0.0

“… 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
some
sandboxy container module. I use it to prevent namespace collision
with an
external library. It’s a hack. Heed the version number.”

It works by reading the .rb file being required, and evaling the
result.

Eero S. vaguely recalled someone posting an alternative
implementation of this concept - probably thinking of Wrapped Modules
by
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
find
out all the top-level constants, then calling require, followed by
Module.constants again to find which constants have been added. It
then
`moves’ those constants under the desired namespace.

Both implementations cause problems in some cases. The most
significant
(and intractible) one seems to be the situation when the require’d
file
includes code like the following:

foo.rb

class String
def hello
“hello #{self}”
end
end

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
different
if the class was called Util, and had just happened to be defined by
another library?)

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
the
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
wanted
to print e.g. “9.76” instead of “9.756”.

The immediate answer to the question is to use sprintf (or printf)
for
rounding:

amount = 9.756
rounded = sprintf(“%.2f”, amount) # == “9.76”

Several people pointed out though that you shouldn’t use Floats to
represent currency.

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
given
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
range
of values - much wider than their integer counterparts. This is
handled
through an exponent and mantissa. For this ability, they trade off
precision.”

The Ruby solution for exact decimals is BigDecimal:

require ‘bigdecimal’

a = BigDecimal.new(“0.2”)
b = BigDecimal.new(“0.05”)

c = a / b

Operations with BigDecimal are always exact (but slower than with
Float),
and so it is safe to use with currencies and other situations where
any
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)
while another_turn?(b,m)
b = simulate(b,m)
end

James Edward G. II suggested loop, break unless, as in

loop do
b = simulate(b,m)
break unless another_turn?(b,m)
end

Ruby does support the following, which is just like a “do-while” loop
in
other languages, however Matz recently said on the ruby-core mailing
list
“Don’t use it please. I’m regretting this feature, and I’d like to
remove
it in the future if it’s possible.”

begin
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
about
this a while ago, but was told that only Java and BREW were currently
supported.

Gene T. noted that Python is making progress in this area, linking
to a
Python for Mobile Devices page, which lists a number of ports
including
official Nokia support and downloads for “Series 60” phones. (The
first
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
library.

| A few other projects have the line with `Ncurses::mousemask…’ in
them
| - but it’s always commented out… like they couldn’t get it
working
| either.

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.

On Jan 3, 2006, at 3:27 AM, Tim S. wrote:

http://www.rubyweeklynews.org/20060101.html

Ruby Weekly News 26th December 2005 - 1st January 2006

[snip]

   great to see some solid Ruby entries this year. Ruby Q.  

will take a
break for the contest, to encourage others to enter and give
me time
to make my own entry."

Uh, Tim… I said that in February of 2005 about last year’s
contest… :wink:

http://rubyurl.com/EhL

James Edward G. II

James Edward G. II wrote:

On Jan 3, 2006, at 3:27 AM, Tim S. wrote:

http://www.rubyweeklynews.org/20060101.html
[…]
James Edward G. II said that the International Conference of
Functional Programming have announced the dates for 2006’s
programming contest.
[…]
Uh, Tim… I said that in February of 2005 about last year’s
contest… :wink:

Oh dear.

Also, the “Active Record” and “Ruby on the mobile” threads were both
from July 2005.

My google-groups-screen-scraping-script-breaketh.

On 1/3/06, [email protected] [email protected] wrote:

Oh dear.

Also, the “Active Record” and “Ruby on the mobile” threads were both
from July 2005.

No wonder I was so confused, because I read ruby-talk over the
holidays and didn’t remember much of that!

My google-groups-screen-scraping-script-breaketh.

Y2K6 bug? :wink:

Ryan