루비에서 코드내용과 그 결과 보이기

Tags:

Ruby one liners

코드내용과 그 결과 보이기. 비슷한 것이 xmp라고 있지만, 이것도 좋군요.

irb(main):001:0> def show(&blk)
irb(main):002:1>   printf("%-25s>>  %s\n", expr = blk.call, eval(expr, blk.binding).inspect)
irb(main):003:1> end

사용은

irb(main):005:0> show{ %{ a = [1,2,3] } }
 a = [1,2,3]             >>  [1, 2, 3]
=> nil
irb(main):006:0> show{ %{ a + [3,4] } }
 a + [3,4]               >>  [1, 2, 3, 3, 4]
=> nil
irb(main):007:0>

이런식.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *