[SOLUTION] Lisp Game (#49)

Adam,

PS: Is there a better way to do the following?
Can it be done in a single line?:

def describe_set set
  s=""
  set.each {|o| s+= "There is a #{o} here. " }
  s
end

    How about:

def describe_set(set)
  set.inject("") {|s,o| s += "There is a ${o} here. "}
end

    - Warren Brown

You don't need the assignment. #inject automatically sets 's' to the
previous value of the block:

set.inject("") {|s,o| s + "There is a ${o} here. "}

The assignment doesn't hurt anything, but it obscures what's really
happening. For example, this would give the wrong answer:

set.inject("") {|s,o| s += "There is a #{o} here. "; print "Looping\n"}

regards,
Ed

···

On Tue, Oct 04, 2005 at 04:00:59AM +0900, Warren Brown wrote:

def describe_set(set)
  set.inject("") {|s,o| s += "There is a ${o} here. "}
end