sloth 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data.tar.gz.sig +1 -0
  2. data/README.md +66 -0
  3. data/lib/sloth.rb +82 -0
  4. metadata +75 -0
  5. metadata.gz.sig +0 -0
@@ -0,0 +1 @@
1
+ b�3�/�e�0�H�y��g@$�X�C֬=|E��PX�9���t�,#�<4?�o�Vݪ-Va*NA�eRE� jz[��ۙT��'�l놙�ݶO�6���슌ߛ�� ��3bz�VO ����7U�!�����1�p�^jK���UԐ���^_0�9uGۣS�����e��V�+��Tr��7n�g��w:鞝D/��fE�l ���)�cm=�NkX�*��V q�
@@ -0,0 +1,66 @@
1
+ Sloth
2
+ =====
3
+
4
+ Sloth is a Ruby gem that implements the `lazy()` function as robustly as
5
+ possible.
6
+
7
+ Installation
8
+ ------------
9
+
10
+ gem install sloth
11
+
12
+ Usage
13
+ -----
14
+
15
+ require 'sloth'
16
+ obj = lazy{:sloth}
17
+ obj.to_s # => 'sloth'
18
+
19
+ Hunting Sloths
20
+ --------------
21
+
22
+ Sloths can only (easily) be detected by the `sloth?` method.
23
+
24
+ sloth = lazy{:sloth}
25
+
26
+ sloth.sloth? # => true
27
+ sloth.class # => Symbol
28
+ :sloth.sloth? # => false
29
+ sloth.is_a?(Sloth) # => false
30
+ Sloth === sloth # => false
31
+
32
+ The `evaluated?` method can tell whether a Sloth has been evaluated.
33
+
34
+ sloth = lazy{'sloth'}
35
+
36
+ sloth.evaluated? # => false
37
+ "lazy #{sloth}s"
38
+ sloth.evaluated? # => true
39
+
40
+ nil and false
41
+ -------------
42
+
43
+ Unfortunately, Ruby doesn't provide any easy way to override the behavior of if,
44
+ so
45
+
46
+ if lazy{false}
47
+ puts('fail')
48
+ else
49
+ puts('success')
50
+ end
51
+
52
+ will `puts('fail')`. A workaround has been implemented via the `self()` method,
53
+ which has been defined on `Object`. Therefore,
54
+
55
+ if lazy{false}.self
56
+ puts('fail')
57
+ else
58
+ puts('success')
59
+ end
60
+
61
+ will `puts('success')`.
62
+
63
+ License
64
+ -------
65
+
66
+ Sloth is in the public domain.
@@ -0,0 +1,82 @@
1
+ # A Sloth is just a wrapper to lazily evaluate an object. A Sloth will behave
2
+ # just like the result of the given block except in the case of if statements,
3
+ # where a Sloth object will always evaluate to true, and when the initialize()
4
+ # function is called.
5
+ class Sloth < BasicObject
6
+ def initialize(&block)
7
+ raise(LocalJumpError, 'no block given') unless block
8
+ @block = block
9
+ end
10
+
11
+ def method_missing(name, *args, &block)
12
+ unless defined?(@object)
13
+ @object = @block.call()
14
+ @block = nil
15
+ end
16
+
17
+ begin
18
+ begin
19
+ @object.public_send(name, *args, &block)
20
+ rescue ::NoMethodError => _ex_
21
+ if _ex_.backtrace[0].start_with?(__FILE__)
22
+ # If @object doesn't respond to public_send(), try using __send__().
23
+ @object.__send__(name, *args, &block)
24
+ else
25
+ ::Kernel.raise(_ex_)
26
+ end
27
+ end
28
+ rescue ::Exception => _ex_
29
+ # Remove evidence of ourself from the traceback.
30
+ _ex_.backtrace.reject!{|line| line.start_with?(__FILE__)}
31
+ ::Kernel.raise(_ex_)
32
+ end
33
+ end
34
+
35
+ instance_methods.each do |method_name|
36
+ next if [:method_missing, :initialize].include?(method_name)
37
+
38
+ define_method(method_name) do |*args, &block|
39
+ method_missing(method_name, *args, &block)
40
+ end
41
+ end
42
+
43
+ # sloth? will return true for Sloth instances and false for everything else.
44
+ # This is basically the only way to tell if something is a Sloth.
45
+ def sloth?
46
+ true
47
+ end
48
+
49
+ # This is true iff we have already been evaluated.
50
+ def evaluated?
51
+ defined?(@object)
52
+ end
53
+ end
54
+
55
+ class Object
56
+ # Return self.
57
+ def self
58
+ self
59
+ end
60
+
61
+ # sloth? will return true for Sloth instances and false for everything else.
62
+ # This is basically the only way to tell if something is a Sloth.
63
+ def sloth?
64
+ false
65
+ end
66
+ end
67
+
68
+ class Class
69
+ alias :__original_equals_equals_equals, :===
70
+
71
+ # Redefine ===() so that it will operate properly on Sloth instances.
72
+ def ===(obj)
73
+ __original_equals_equals_equals(obj.instance_eval{self})
74
+ end
75
+ end
76
+
77
+ module Kernel
78
+ # Return a Sloth initialized with the given block.
79
+ def lazy(&block)
80
+ Sloth.new(&block)
81
+ end
82
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sloth
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - katmagic
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain:
12
+ - !binary |-
13
+ LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURRRENDQWlpZ0F3SUJB
14
+ Z0lCQURBTkJna3Foa2lHOXcwQkFRVUZBREJHTVJnd0ZnWURWUVFEREE5MGFH
15
+ VXUKYldGbmFXTmhiQzVyWVhReEZUQVRCZ29Ka2lhSmsvSXNaQUVaRmdWbmJX
16
+ RnBiREVUTUJFR0NnbVNKb21UOGl4awpBUmtXQTJOdmJUQWVGdzB4TVRBNE1q
17
+ RXlNak15TURGYUZ3MHhNakE0TWpBeU1qTXlNREZhTUVZeEdEQVdCZ05WCkJB
18
+ TU1EM1JvWlM1dFlXZHBZMkZzTG10aGRERVZNQk1HQ2dtU0pvbVQ4aXhrQVJr
19
+ V0JXZHRZV2xzTVJNd0VRWUsKQ1pJbWlaUHlMR1FCR1JZRFkyOXRNSUlCSWpB
20
+ TkJna3Foa2lHOXcwQkFRRUZBQU9DQVE4QU1JSUJDZ0tDQVFFQQpwQnQyMG53
21
+ anM1VzAzZGpwUk42RkFicGlpbzI4Nk5ITVRrNkhobWpWNkdaS09pNVpVWDVv
22
+ blRuS1VnMlZjMzV6Ci9uSythSVBSZXlSZkJnSWNmU2pob1hoMUExRHArMmxh
23
+ TmdUdFUvM2VNdXBydWF0Z09SQVBDU2FHOU5zK0hTeVIKdnlTYnoxUVVyd3Zs
24
+ dkYwcWtoaEFwTlE2ZHNMbDJMTU9WM1FjbHVZK1kzQ1ZjY09XT1NIZFFjbkFi
25
+ UHV6TTlIZgo0Q2hJNE9HTDcrRHdMQTVPSzJTNXVld1JBYTJpTGtKU04wV3Vn
26
+ blFsSnFNVDU5R1JhcVRET3RuWVFwaXlLRUJ5ClFqVlBPNExOazdpRHNKUDIy
27
+ WUJydmVJem04L1lZUkJUVTRMVEhNRU1PeUNzenJZcUQyUzFMd3AycnRDSnpR
28
+ Q2wKQkEwTHRCS3JabDVtd1ptN3F5aitUd0lEQVFBQm96a3dOekFKQmdOVkhS
29
+ TUVBakFBTUIwR0ExVWREZ1FXQkJTbQpzNWFyaGpwNjFrbUdsNndzbUxZa3Fl
30
+ cmRxREFMQmdOVkhROEVCQU1DQkxBd0RRWUpLb1pJaHZjTkFRRUZCUUFECmdn
31
+ RUJBQTZjUU5RTU9QUnk0eXJqN05oNU1iOXFxOHQvOGhvL0pRdmp6Vm9mOXFS
32
+ ZCtrZktyT29PaFhmRU8rUm0Kc1djYU9uQkNWQzREblp1TkRTTHlnVmhDRHRN
33
+ bkhqZy9Kc2ZPL0dCRi9RbE5USk9PMWprb1FpUzZ3MEtBUmxCbQpjcFhhV2cv
34
+ b010WEoyUGFVZ2E2V2tOZVhZZjlNYWQzNlA0eXVHUVNjanMrV2tVVXk3RE5a
35
+ dlRHUmVJY0NXT1I4Cmp0ZVN2dkNNb2JRS0dyMkRmRk9VOUppZGRoMkZQcHov
36
+ S09NMmlqendzVk5VTVVyN1I1OExvQ25RWnJaL1lhUlcKb2I2UW5WZ3dxdTVT
37
+ VUFLUXhsRkovYUtsUE1qNzM1ejhFb2dhWkMxWkhnZzN2a2dHR3l1NTdOLzhC
38
+ RERHMFR6QwpabjN1MmxlVmFlL2ZKMDN6WUdBcmh1SktQZ2M9Ci0tLS0tRU5E
39
+ IENFUlRJRklDQVRFLS0tLS0K
40
+ date: 2012-05-08 00:00:00.000000000 Z
41
+ dependencies: []
42
+ description: Sloth makes laziness easy.
43
+ email: the.magical.kat@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - lib/sloth.rb
49
+ - README.md
50
+ homepage: https://github.com/chloe/sloth
51
+ licenses: []
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubyforge_project:
70
+ rubygems_version: 1.8.10
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Lazily evaluate things.
74
+ test_files: []
75
+ has_rdoc:
Binary file