Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Temporary execution context with included modules

I needed to extend "self" with a module but only wanted the included methods to stick around "temporarily". After a lot of futile attempts with binding, callcc, eval, etc... I found it was as simple as "self.dup.instance_eval { ... } ":

   1  
   2  def foo
   3    'bar'
   4  end
   5  
   6  module M
   7    def foo
   8      'baz'
   9    end
  10  end
  11  
  12  puts "should be 'bar': #{foo}"
  13  
  14  self.dup.instance_eval do
  15    extend(M)
  16    puts "should be 'baz': #{foo}"
  17  end
  18  
  19  puts "should be 'bar': #{foo}"

dup, make-blank-value, and shift functions

   1  
   2      ; used in SHIFT below
   3  	dup: func [value len [integer!] /local type] [
   4          type: either series? value [value] [either char? value [""] [[]]]
   5  		head insert/only/dup make type len value len
   6  	]
   7  
   8      ; used in SHIFT below
   9      make-blank-value: func [type] [
  10          any [
  11              attempt [make type 0]
  12              attempt [make type ""]
  13              attempt [make type []]
  14              attempt [make type none]
  15          ]
  16      ]
  17  
  18      shift: func [
  19          "Shift values in a series; length doesn't change."
  20          series [series!]
  21          /left   "Shift left (the default)"
  22          /right  "Shift right"
  23          /part range [number!] "Shift this many positions"  ; TBD series! support?
  24          /with fill "Fill vacated slots with this value"
  25          /local pad
  26      ][
  27          range: any [range 1]
  28          if any [empty? series  0 = range] [return series]
  29          pad: dup any [fill make-blank-value last series] range
  30          either right [
  31              head insert head clear skip tail series negate range pad
  32          ][
  33              append remove/part series range pad
  34          ]
  35      ]
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS