From: "James M. Lawrence" Date: 2008-12-11T12:53:10+09:00 Subject: Re: P shows object values, so can u put P and value in txt? I'd like to randomly interject the under-appreciated technique of printing an expression and its value, as opposed just the value. def show(&block) expression = block.call.strip result = eval(expression, block.binding) printf("%-20s #=> %s\n", expression, result.inspect) end An example using the Array#slice docs, a = [ "a", "b", "c", "d", "e" ] show {%{ a[2] + a[0] + a[1] }} show {%{ a[6] }} show {%{ a[1, 2] }} show {%{ a[1..3] }} show {%{ a[4..7] }} show {%{ a[6..10] }} show {%{ a[-3, 3] }} verbatim output: a[2] + a[0] + a[1] #=> "cab" a[6] #=> nil a[1, 2] #=> ["b", "c"] a[1..3] #=> ["b", "c", "d"] a[4..7] #=> ["e"] a[6..10] #=> nil a[-3, 3] #=> ["c", "d", "e"] (Binding#of_caller could be used to circumvent the need for the block, but that has problems of its own.) -- Posted via http://www.ruby-forum.com/.