From: "Ara.T.Howard" Date: 2005-10-05T14:21:48+09:00 Subject: Re: Monitoring total memory usage? On Wed, 5 Oct 2005, Andrew Backer wrote: > Wow, quick responses ;) > > No, i don't know about PP. I am just accustomed to having serious > output formatting control in fortran, c & derivitives, etc. for > printing ints, floats, hex, padding, ... Its probably here, I just > don't know where it is. harp:~ > irb irb(main):001:0> printf "%s\n", "foobar" foobar irb(main):002:0> printf "%b\n", 42 101010 irb(main):004:0> puts( '|' << ('%s' % 'foobar').center(42) << '|') | foobar | all the printf modifiers, and more, are there. class string also has many formatting features. > I just extend the class for each individual program. I wrote the resize so > I don't have to worry about what arrays I pass into the function. > resize(n,def) just .clear's it and stuffs a bunch of 'def' in to up to size > n. I haven't yet writting large ruby programs, so no experience worrying > about things. > > Array *index* wrapping is useful in some cases (fibonacci numbers with > no extra assignments). However, most of the time when I accidentially > go past the lower bounds I would like to know since a[x-1] wasn't > intended to set the value at a[9], but since x was zero it did. > > nums = [1,2,3] # nums[-1] = 3 mums[2] = 3, nums[-2] = 2, etc > > def calc_fib(n) > a = [0,1,0] > for i in 2..n > k = i%3 > a[k] = a[k-1] + a[k-2] > end > return a[n%3] > end harp:~ > cat a.rb class Array alias_method '__idx__', '[]' def [] *a, &b n = a.first raise RangeError, "negative index <#{ n }>" if Numeric === n and n < 0 send '__idx__', *a, &b end end a = [] a << 42 p a[0] p a[0, 1] p a[0 .. 0] p a[-1] harp:~ > ruby a.rb 42 [42] [42] a.rb:6:in `[]': negative index <-1> (RangeError) from a.rb:16 hth. -a -- =============================================================================== | email :: ara [dot] t [dot] howard [at] noaa [dot] gov | phone :: 303.497.6469 | Your life dwells amoung the causes of death | Like a lamp standing in a strong breeze. --Nagarjuna ===============================================================================