Dave Burt
3/29/2006 9:15:00 AM
Kev Jackson wrote:
> My original goal was to allow the qc team to write
>
> text :id, :pg_401, :name
>
> So the arguments in theory should be symbols not strings, hence the need
> for some kind of to_s/#{} hackery
That's fair enough, though I'd prefer to either use strings or change the
$test_data hash to avoid having to convert. For instance, you could use a
HashWithIndifferentAccess from Rails' Active Support module (require
'active_support'), or you could just convert the keys to symbols as you load
the data.
> ...
> However, coming from a Java background, "s1" + "s2" is a performance
> (memory/speed) nightmare, so I shy away from S1 + S2 and try to use
> #{S1}S2 where possible. Perhaps string + string isn't so bad in Ruby, but
> old habits die hard...
It's not so terrible in Java either; when you want to avoid it is where
you're adding multiple times to get a single result string, especially in a
big loop. Then you'd use a StringBuffer to avoid creating and
garbage-collecting zillions of strings. Of course, Ruby's strings are
mutable, so you don't need another class of object.
Compare:
s = ""; 50.times {|i| s += i.to_s + " "} # watch out: 50 strings are
discarded
with:
s = ""; 50.times {|i| s << i.to_s << " " } # like Java's StringBuffer: one
object, efficient
Anyway, in this case, "#{x}y" is about the same as x + "y", because you're
only creating one extra string. You've just got the added method-call
overhead for String#+.
> Thanks
You're welcome.
Cheers,
Dave