--------------010600040706040805070703
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
I've extended mark sparshatt's code and wrote an UnitTest.
const_def.rb is the code and const_def_test.rb the test
Examples:
class MyClass
const_def 4, :CONST_A1, :CONST_A2, :CONST_A3
p CONST_A1, CONST_A2, CONST_A3
end
#=>
4
5
6
class MyClassTwo
const_def "a",:CONST_A1, :CONST_A2, 7, :CONST_A3
p CONST_A1, CONST_A2, CONST_A3
end
#=>
"a"
"b"
7
--
Jannis Harder
--------------010600040706040805070703
Content-Type: text/plain; x-mac-type="2A2A2A2A"; x-mac-creator="48647261";
name="const_def_test.rb"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="const_def_test.rb"
require 'const_def'
require 'test/unit'
class ConstDefTest < Test::Unit::TestCase
def test_integers
c = Class.new
c.instance_eval do
const_def :CONST_A1, :CONST_A2, :CONST_A3
const_def 10, :CONST_B1, :CONST_B2, :CONST_B3
const_def 10, :CONST_C1, 2, :CONST_C2, 1, :CONST_C3
end
assert_equal([c::CONST_A1,c::CONST_A2,c::CONST_A3],[1, 2 ,3 ])
assert_equal([c::CONST_B1,c::CONST_B2,c::CONST_B3],[10,11,12])
assert_equal([c::CONST_C1,c::CONST_C2,c::CONST_C3],[10,2 ,1 ])
end
def test_floats
c = Class.new
c.instance_eval do
const_def 1.5, :CONST_A1, :CONST_A2, :CONST_A3
const_def 2.5, :CONST_B1, 7.0, :CONST_B2, :CONST_B3
const_def 3.5, :CONST_C1, 2.5, :CONST_C2, 0.5, :CONST_C3
end
assert_equal([c::CONST_A1,c::CONST_A2,c::CONST_A3],[1.5, 2.5 ,3.5 ])
assert_equal([c::CONST_B1,c::CONST_B2,c::CONST_B3],[2.5, 7.0 ,8.0 ])
assert_equal([c::CONST_C1,c::CONST_C2,c::CONST_C3],[3.5, 2.5 ,0.5 ])
end
def test_strings
c = Class.new
c.instance_eval do
const_def "jix", :CONST_A1, :CONST_A2, :CONST_A3
const_def "ruby", :CONST_B1, "zzz", :CONST_B2, :CONST_B3
const_def "hello", :CONST_C1, "world", :CONST_C2, "!", :CONST_C3
end
assert_equal([c::CONST_A1,c::CONST_A2,c::CONST_A3],%w{jix jiy jiz})
assert_equal([c::CONST_B1,c::CONST_B2,c::CONST_B3],%w{ruby zzz aaaa})
assert_equal([c::CONST_C1,c::CONST_C2,c::CONST_C3],%w{hello world !})
end
def test_custom
c = Class.new
c.instance_eval do
const_def CustomClass.new(1,1), :CONST_A1,
:CONST_A2,
:CONST_A3
const_def CustomClass.new(3,1), :CONST_B1,
CustomClass.new("a","a"), :CONST_B2,
:CONST_B3
const_def CustomClass.new(1,2), :CONST_C1,
CustomClass.new(3,4), :CONST_C2,
CustomClass.new(5,6), :CONST_C3
end
assert_equal([c::CONST_A1,c::CONST_A2,c::CONST_A3],[
CustomClass.new(1,1),
CustomClass.new(1,3),
CustomClass.new(2,3)
])
assert_equal([c::CONST_B1,c::CONST_B2,c::CONST_B3],[
CustomClass.new(3,1),
CustomClass.new("a","a"),
CustomClass.new("a","c")
])
assert_equal([c::CONST_C1,c::CONST_C2,c::CONST_C3],[
CustomClass.new(1,2),
CustomClass.new(3,4),
CustomClass.new(5,6)
])
end
def test_mixed
c = Class.new
current_time = Time.new
c.instance_eval do
const_def 1, :CONST_A1, :CONST_A2, "hi",:CONST_A3
const_def current_time, :CONST_B1, 50, :CONST_B2, :CONST_B3
const_def 3.141, :CONST_C1, [1,2], :CONST_C2, "!", :CONST_C3
end
assert_equal([c::CONST_A1,c::CONST_A2,c::CONST_A3],[1, 2, "hi"])
assert_equal([c::CONST_B1,c::CONST_B2,c::CONST_B3],[current_time, 50, 51 ])
assert_equal([c::CONST_C1,c::CONST_C2,c::CONST_C3],[3.141, [1,2], "!" ])
end
class CustomClass
include Comparable
attr_accessor :a,:b
def initialize(a,b)
@a = a
@b = b
end
def succ
if @a < @b
CustomClass.new(@a.succ,@b)
else
CustomClass.new(@a,@b.succ.succ)
end
end
def <=> other
if (res = (@a <=> other.a)) == 0
@b <=> other.b
else
res
end
end
def inspect
"<#CC #{@a.inspect} #{@b.inspect}>"
end
end
end
--------------010600040706040805070703
Content-Type: text/plain; x-mac-type="2A2A2A2A"; x-mac-creator="48647261";
name="const_def.rb"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="const_def.rb"
class Class
def const_def(*symbols)
val = 1
symbols.each_with_index do |symbol, index|
unless symbol.is_a? Symbol
val = symbol
else
const_set(symbol, val)
begin
if val.respond_to? :succ
val = val.succ
else
val+=1
end
rescue
val = nil
end
end
end
end
end
--------------010600040706040805070703--