J
John Carter
[Note: parts of this message were removed to make it a legal post.]
Here are two idioms which you may find useful, or at least curious, when
trying to optimize the heck out of a Ruby script....
require 'pp'
# Similar to the "autosequence" facility in SQL.
# Useful for replacing a complex key with a very simply POD proxy object.
file_sequence = 0
file_index = Hash.new{|hash,key| file_sequence+=1;hash[key] = file_sequence}
p file_index["foo"]
p file_index["foo"]
p file_index["bah"]
p file_index["foo"]
p file_index["bah"]
pp file_index
# Similar to "to_sym", but can cope with spaces and weird characters...
# So if tom1, tom2, tom3... go out of scope they can be garbage collected...
# so if you end up just holding tomtom, you have only one copy of "tom" and
you can test for
# equality with .object_id == .object_id!
one_true_string = Hash.new{|hash,string| hash[string] = string}
tom1 = "tom"
tom2 = "tom"
tom3 = "#{tom1}"
tom4 = tom2.clone
tom5 = "t"+"o"+"m"
toms = [tom1, tom2, tom3, tom4, tom5]
pp toms
pp toms.collect{|tom| tom.object_id }
tomtom = toms.collect{|tom| one_true_string[tom] }
pp tomtom
pp tomtom.collect{|tom| tom.object_id }
Running the above results in...
1
1
2
1
2
{"foo"=>1, "bah"=>2}
["tom", "tom", "tom", "tom", "tom"]
[-609503848, -609503908, -609503888, -609503918, -609503978]
["tom", "tom", "tom", "tom", "tom"]
[-609503848, -609503848, -609503848, -609503848, -609503848]
--
John Carter Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : (e-mail address removed)
New Zealand
=======================================================================
This email, including any attachments, is only for the intended
addressee. It is subject to copyright, is confidential and may be
the subject of legal or other privilege, none of which is waived or
lost by reason of this transmission.
If the receiver is not the intended addressee, please accept our
apologies, notify us by return, delete all copies and perform no
other act on the email.
Unfortunately, we cannot warrant that the email has not been
altered or corrupted during transmission.
=======================================================================
Here are two idioms which you may find useful, or at least curious, when
trying to optimize the heck out of a Ruby script....
require 'pp'
# Similar to the "autosequence" facility in SQL.
# Useful for replacing a complex key with a very simply POD proxy object.
file_sequence = 0
file_index = Hash.new{|hash,key| file_sequence+=1;hash[key] = file_sequence}
p file_index["foo"]
p file_index["foo"]
p file_index["bah"]
p file_index["foo"]
p file_index["bah"]
pp file_index
# Similar to "to_sym", but can cope with spaces and weird characters...
# So if tom1, tom2, tom3... go out of scope they can be garbage collected...
# so if you end up just holding tomtom, you have only one copy of "tom" and
you can test for
# equality with .object_id == .object_id!
one_true_string = Hash.new{|hash,string| hash[string] = string}
tom1 = "tom"
tom2 = "tom"
tom3 = "#{tom1}"
tom4 = tom2.clone
tom5 = "t"+"o"+"m"
toms = [tom1, tom2, tom3, tom4, tom5]
pp toms
pp toms.collect{|tom| tom.object_id }
tomtom = toms.collect{|tom| one_true_string[tom] }
pp tomtom
pp tomtom.collect{|tom| tom.object_id }
Running the above results in...
1
1
2
1
2
{"foo"=>1, "bah"=>2}
["tom", "tom", "tom", "tom", "tom"]
[-609503848, -609503908, -609503888, -609503918, -609503978]
["tom", "tom", "tom", "tom", "tom"]
[-609503848, -609503848, -609503848, -609503848, -609503848]
--
John Carter Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : (e-mail address removed)
New Zealand
=======================================================================
This email, including any attachments, is only for the intended
addressee. It is subject to copyright, is confidential and may be
the subject of legal or other privilege, none of which is waived or
lost by reason of this transmission.
If the receiver is not the intended addressee, please accept our
apologies, notify us by return, delete all copies and perform no
other act on the email.
Unfortunately, we cannot warrant that the email has not been
altered or corrupted during transmission.
=======================================================================