ludy 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/ludy.rb ADDED
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # Copyright (c) 2007, Lin Jen-Shin�]a.k.a. godfat �u�`�^
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ def require_ludy target = nil
18
+ if target
19
+ require(File.join(File.dirname(__FILE__), 'ludy', target))
20
+ else
21
+ require_all_in_dir __FILE__, 'ludy'
22
+ end
23
+ end
24
+
25
+ def require_all_in_dir target_file, *dirs
26
+ dir = File.join(File.dirname(target_file), *dirs)
27
+ Dir.foreach(dir){ |f|
28
+ next unless f =~ /.rb$/
29
+ require(File.join(dir, f))
30
+ }
31
+ end
@@ -0,0 +1,39 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # Copyright (c) 2007, Lin Jen-Shin(a.k.a. godfat 真常)
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ module Ludy
18
+ TRACE_EVENT = 0
19
+ TRACE_FILE = 1
20
+ TRACE_LINE = 2
21
+ TRACE_MSG = 3
22
+ TRACE_BINDING = 4
23
+ TRACE_CLASS = 5
24
+
25
+ def callstack levels = -1
26
+ st = Thread.current[:callstack]
27
+ if levels then st && st[levels - 2] else st end
28
+ end
29
+ module_function :callstack
30
+ end # of Ludy
31
+
32
+ set_trace_func lambda{ |*args|
33
+ case args[Ludy::TRACE_EVENT]
34
+ when /call$/
35
+ (Thread.current[:callstack] ||= []).push args
36
+ when /return$/
37
+ (Thread.current[:callstack] ||= []).pop
38
+ end
39
+ }
data/lib/ludy/dice.rb ADDED
@@ -0,0 +1,68 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # Copyright (c) 2007, Lin Jen-Shin(a.k.a. godfat 真常)
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ module Ludy
18
+
19
+ class Dice
20
+ attr_reader :amounts, :faces
21
+ def initialize amounts = 1, faces = 20
22
+ @amounts = amounts
23
+ @faces = faces
24
+ end
25
+ def roll
26
+ @amounts.roll @faces
27
+ end
28
+ def min; @amounts; end
29
+ def max; @amounts*@faces; end
30
+ end
31
+
32
+ class DiceSet
33
+ attr_reader :min, :max
34
+ def initialize *args
35
+ @diceset = args
36
+ @min = @max = 0
37
+ @diceset.each{ |i|
38
+ @min += i.min
39
+ @max += i.max
40
+ }
41
+ end
42
+ def roll
43
+ result = 0
44
+ @diceset.each { |i| result += i.roll }
45
+ result
46
+ end
47
+ def << dice
48
+ @diceset << dice
49
+ @min += dice.min
50
+ @max += dice.max
51
+ self
52
+ end
53
+ end
54
+
55
+ end
56
+
57
+ class Numeric
58
+ def roll faces = 20
59
+ return nil unless self > 0 && self.integer? &&
60
+ faces > 0 && faces.integer?
61
+ result = 0
62
+ 1.step(self){ |i| result += rand(faces) + 1 }
63
+ result
64
+ end
65
+ def dice faces = 20
66
+ Ludy::Dice.new self, faces
67
+ end
68
+ end
data/lib/ludy/lazy.rb ADDED
@@ -0,0 +1,39 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # Copyright (c) 2007, Lin Jen-Shin(a.k.a. godfat 真常)
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ module Ludy
18
+
19
+ class Lazy
20
+ instance_methods.each{|m| undef_method m unless m =~ /^__/}
21
+
22
+ def initialize func = nil, &block
23
+ if block_given? then @func = block
24
+ else
25
+ raise TypeError, "#{func} don't respond to :call" unless func.respond_to? :call
26
+ @func = func
27
+ end
28
+ end
29
+
30
+ def method_missing msg, *arg, &block
31
+ (@obj ||= @func.call).__send__ msg, *arg, &block
32
+ end
33
+ end
34
+
35
+ def lazy arg = nil, &block
36
+ Lazy.new arg, &block
37
+ end
38
+
39
+ end # of Ludy
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # Copyright (c) 2007, Lin Jen-Shin(a.k.a. godfat 真常)
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ class Object
18
+ def tap
19
+ yield self
20
+ self
21
+ end
22
+ def define_instance_method msg, &block
23
+ self.class.send :define_method, msg, &block
24
+ end
25
+ def alias_instance_method new_msg, old_msg
26
+ self.class.send :alias_method, new_msg, old_msg
27
+ end
28
+ end
29
+
30
+ class NilClass
31
+ def method_missing msg, *arg, &block
32
+ self
33
+ end
34
+ end
@@ -0,0 +1,41 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # Copyright (c) 2007, Lin Jen-Shin(a.k.a. godfat 真常)
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require 'rubygems'
18
+ raise LoadError.new('you need ruby2ruby gem to use this tool') unless require 'ruby2ruby'
19
+ begin
20
+ require_ludy 'ludy_ext'
21
+ rescue NameError
22
+ raise LoadError.new('please require "ludy" first')
23
+ end
24
+
25
+ module Ludy
26
+
27
+ class Rambda
28
+ def initialize &block
29
+ @this = eval block.to_ruby
30
+ define_instance_method :call, &@this
31
+ alias_instance_method :[], :call
32
+ end
33
+ attr_reader :this
34
+ alias_method :to_proc, :this
35
+ end
36
+
37
+ def rambda &block
38
+ Rambda.new &block
39
+ end
40
+
41
+ end # of Ludy
data/lib/ludy/this.rb ADDED
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # Copyright (c) 2007, Lin Jen-Shin(a.k.a. godfat 真常)
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ begin
18
+ require_ludy 'callstack'
19
+ rescue NameError
20
+ raise LoadError.new('please require "ludy" first')
21
+ end
22
+
23
+ module Ludy
24
+ def this
25
+ info = callstack(-2)
26
+ # lambda{ |*args|
27
+ # Thread.current[:temp_args] = args
28
+ # eval("send :#{info[3]}, *Thread.current[:temp_args]", info[4])
29
+ # }
30
+ eval('self', info[TRACE_BINDING]).method(info[TRACE_MSG])
31
+ end
32
+ module_function :this
33
+ end # of Ludy
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # Copyright (c) 2007, Lin Jen-Shin(a.k.a. godfat 真常)
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ module Ludy
18
+
19
+ class Variable
20
+ instance_methods.each{|m| undef_method m unless m =~ /^__/}
21
+
22
+ def initialize obj
23
+ @__obj__ = obj
24
+ end
25
+
26
+ def method_missing msg, *arg, &block
27
+ @__obj__.__send__ msg, *arg, &block
28
+ end
29
+
30
+ attr_accessor :__obj__
31
+ end
32
+
33
+ def var arg
34
+ Variable.new arg
35
+ end
36
+
37
+ end # of Ludy
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # Copyright (c) 2007, Lin Jen-Shin(a.k.a. godfat 真常)
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ begin
18
+ require_ludy 'lazy'
19
+ rescue NameError
20
+ raise LoadError.new('please require "ludy" first')
21
+ end
22
+
23
+ module Ludy
24
+
25
+ Y = lambda{|f|
26
+ lambda{|x| lazy{f[x[x]]} }[lambda{|x| lazy{f[x[x]]} }]
27
+ }
28
+
29
+ end
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # Copyright (c) 2007, Lin Jen-Shin(a.k.a. godfat 真常)
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ module Ludy
18
+
19
+ Z = lambda{|f|
20
+ lambda{|x| f[lambda{|y| x[x][y]}]}[
21
+ lambda{|x| f[lambda{|y| x[x][y]}]}
22
+ ]
23
+ }
24
+
25
+ end
data/ludy.gemspec ADDED
@@ -0,0 +1,44 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # Copyright (c) 2007, Lin Jen-Shin(a.k.a. godfat 真常)
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require 'rubygems'
18
+
19
+ spec = Gem::Specification.new{|s|
20
+ s.name = 'ludy'
21
+ s.version = '0.0.1'
22
+ s.author = 'Lin Jen-Shin(a.k.a. godfat)'
23
+ s.email = 'strip number: 135godfat7911@246gmail.890com'
24
+ s.homepage = 'http://ludy.rubyforge.org/'
25
+ s.platform = Gem::Platform::RUBY
26
+ s.summary = 'Aims to extend Ruby standard library, providing some useful tools that\'s not existed in the standard library.'
27
+ candidates = Dir.glob '{bin,doc,lib,test}/**/*'
28
+ candidates+= Dir.glob '*'
29
+ s.files = candidates.delete_if{|item|
30
+ item.include?('CVS') || item.include?('rdoc') || File.extname(item) == '.gem'
31
+ }
32
+
33
+ s.require_path = 'lib'
34
+ s.autorequire = 'ludy'
35
+ s.test_file = 'test/ts_ludy.rb'
36
+ s.has_rdoc = false
37
+ # s.extra_rdoc_files = []
38
+ # s.add_dependency 'multi', '>=0.1'
39
+ }
40
+
41
+ if $0 == __FILE__
42
+ Gem::manage_gems
43
+ Gem::Builder.new(spec).build
44
+ end
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # Copyright (c) 2007, Lin Jen-Shin(a.k.a. godfat 真常)
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require 'test/unit'
18
+ require(File.join(File.dirname(__FILE__), '..', 'lib', 'ludy'))
19
+ require_ludy 'callstack'
20
+ include Ludy
21
+ class TestCallstack < Test::Unit::TestCase
22
+ def setup; @binding = 'XD' end
23
+ def test_callstack
24
+ called_line = __LINE__-1
25
+ top = callstack
26
+ assert_equal 'call', top[TRACE_EVENT]
27
+ assert_equal __FILE__, top[TRACE_FILE]
28
+ assert_equal called_line, top[TRACE_LINE]
29
+ assert_equal :test_callstack, top[TRACE_MSG]
30
+ assert_equal 'XD', eval('@binding', top[TRACE_BINDING])
31
+ assert_equal self.class, top[TRACE_CLASS]
32
+ end
33
+ end