From: "Peña, Botp" Date: 2007-01-16T11:16:57+09:00 Subject: Re: Can anyone try to solve this problems? From: Cyrus Gabilla [mailto:gabilla19992004@yahoo.com] : #--------------------------------------------------------------- # I am trying to solve these three problems in Ruby. I do not # know if my solutions are really correct. I just wanted to ask #--------------------------------------------------------------- test it :) #--------------------------------------------------------------- # 1. Write a one-line in irb using Range#inject to calculate # 20!. Generalize this into a function. #--------------------------------------------------------------- fri is your friend C:\Documents and Settings\pe�aijm>fri inject c:0:Warning: require_gem is obsolete. Use gem instead. ------------------------------------------------------ Enumerable#inject enum.inject(initial) {| memo, obj | block } => obj enum.inject {| memo, obj | block } => obj ------------------------------------------------------------------------ Combines the elements of _enum_ by applying the block to an accumulator value (_memo_) and each element in turn. At each step, _memo_ is set to the value returned by the block. The first form lets you supply an initial value for _memo_. The second form uses the first element of the collection as a the initial value (and skips that element while iterating). # Sum some numbers (5..10).inject {|sum, n| sum + n } #=> 45 # Multiply some numbers (5..10).inject(1) {|product, n| product * n } #=> 151200 # find the longest word longest = %w{ cat sheep bear }.inject do |memo,word| memo.length > word.length ? memo : word end longest #=> "sheep" # find the length of the longest word longest = %w{ cat sheep bear }.inject(0) do |memo,word| memo >= word.length ? memo : word.length end longest #=> 5 #--------------------------------------------------------------- # 2. Write a function to find the longest string in an array # of strings. #--------------------------------------------------------------- fri is your friend C:\Documents and Settings\pe�aijm>fri max c:0:Warning: require_gem is obsolete. Use gem instead. ------------------------------------------------------ Multiple choices: Enumerable#max, SizedQueue#max C:\Documents and Settings\pe�aijm>fri enumerable#max c:0:Warning: require_gem is obsolete. Use gem instead. --------------------------------------------------------- Enumerable#max enum.max => obj enum.max {|a,b| block } => obj ------------------------------------------------------------------------ Returns the object in _enum_ with the maximum value. The first form assumes all objects implement +Comparable+; the second uses the block to return _a <=> b_. a = %w(albatross dog horse) a.max #=> "horse" a.max {|a,b| a.length <=> b.length } #=> "albatross" C:\Documents and Settings\pe�aijm> #--------------------------------------------------------------- # 3. Write an iterator function n_times(n) that calls the # given block n times. #--------------------------------------------------------------- again, fri is your friend C:\Documents and Settings\pe�aijm>fri integer#times c:0:Warning: require_gem is obsolete. Use gem instead. ---------------------------------------------------------- Integer#times int.times {|i| block } => int ------------------------------------------------------------------------ Iterates block _int_ times, passing in values from zero to _int_ - 1. 5.times do |i| print i, " " end _produces:_ 0 1 2 3 4 C:\Documents and Settings\pe�aijm>fri iterator c:0:Warning: require_gem is obsolete. Use gem instead. ------------------------------------------------------- Kernel#iterator? block_given? => true or false iterator? => true or false ------------------------------------------------------------------------ Returns +true+ if +yield+ would execute a block in the current context. The +iterator?+ form is mildly deprecated. def try if block_given? yield else "no block" end end try #=> "no block" try { "hello" } #=> "hello" try do "hello" end #=> "hello" hth. kind regards -botp