iota 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.rdoc +30 -0
  2. data/lib/iota.rb +26 -0
  3. data/test/tests.rb +60 -0
  4. metadata +58 -0
data/README.rdoc ADDED
@@ -0,0 +1,30 @@
1
+ =Summary
2
+
3
+ Iota is a Ruby module that emulates the iota keyword(?) from Google's Go
4
+ programming language. Here is the example from golang.org/doc/effective_go.html:
5
+ type ByteSize float64
6
+ const (
7
+ _ = iota; // ignore first value by assigning to blank identifier
8
+ KB ByteSize = 1<<(10*iota);
9
+ MB;
10
+ GB;
11
+ TB;
12
+ PB;
13
+ YB;
14
+ )
15
+ The equivalent Ruby Iota code is:
16
+ require 'iota'
17
+ include Iota
18
+ set_iota 1
19
+ iota(%w(KB MB GB TB PB YB)) do |i|
20
+ 1 << i * 10
21
+ end
22
+ MarkCC gives a simpler example:
23
+ type Color int; const ( RED Color = iota; ORANGE = iota; YELLOW = iota;
24
+ GREEN = iota; BLUE = iota; INDIGO = iota; VIOLET = iota; )
25
+ And the Ruby Iota code:
26
+ RED = iota; ORANGE = iota; YELLOW = iota; GREEN = iota; BLUE = iota; INDIGO = iota; VIOLET = iota
27
+ OK, but that is painfully inefficient. Let's go back to the array notation:
28
+ iota %w(RED ORANGE YELLOW GREEN BLUE INDIGO VIOLET)
29
+ Aaand... that's all. This is of course one of the least innovative things about
30
+ the Go language, but we can perhaps strive to implement more.
data/lib/iota.rb ADDED
@@ -0,0 +1,26 @@
1
+ module Iota
2
+ @@iota = -1
3
+
4
+ def iota(names = nil)
5
+ if names.nil?
6
+ @@iota += 1
7
+ return @@iota
8
+ end
9
+
10
+ values = []
11
+ names.each do |name|
12
+ @@iota += 1
13
+ if block_given?
14
+ Iota.const_set name, yield(@@iota)
15
+ else
16
+ Iota.const_set name, @@iota
17
+ end
18
+ values << Iota.const_get(name.to_sym)
19
+ end
20
+ values
21
+ end
22
+
23
+ def set_iota(value = 0)
24
+ @@iota = value - 1
25
+ end
26
+ end
data/test/tests.rb ADDED
@@ -0,0 +1,60 @@
1
+ require "../lib/iota"
2
+ require "test/unit"
3
+ include Iota
4
+
5
+ class Iota_Basics < Test::Unit::TestCase
6
+ # Using the real test_setup, we get a:
7
+ # "dynamic constant assignment" error.
8
+ #def test_setup
9
+ Zero = iota
10
+ One = iota
11
+ Two = iota
12
+ Three = iota
13
+ #end
14
+
15
+ def test_basic
16
+ assert_equal(0, Zero, "First iota should be 0.")
17
+ assert_equal(1, One, "iota should increment.")
18
+ assert_equal(2, Two, "iota should increment.")
19
+ assert_equal(3, Three, "iota should increment.")
20
+ end
21
+ end
22
+
23
+ class Iota_Arrays < Test::Unit::TestCase
24
+ # Using the real test_setup, we get a:
25
+ # "dynamic constant assignment" error.
26
+ #def test_setup
27
+ set_iota 0
28
+ iota %w(Cero Uno Dos Tres Quatro)
29
+ iota %w(Cinco Seis Siete Ocho)
30
+ #end
31
+
32
+ def test_arrays
33
+ assert_equal(0, Iota::Cero, "First iota in an array should be 0.")
34
+ assert_equal(1, Uno, "iota in an array should increment.")
35
+ assert_equal(2, Dos, "iota in an array should increment.")
36
+ assert_equal(3, Tres, "iota in an array should increment.")
37
+ assert_equal(4, Quatro, "iota in an array should increment.")
38
+ end
39
+
40
+ def test_continuation
41
+ assert_equal(5, Cinco, "iota should continue to increment, array to array.")
42
+ assert_equal(6, Seis, "iota should continue to increment, array to array.")
43
+ assert_equal(7, Siete, "iota should continue to increment, array to array.")
44
+ assert_equal(8, Ocho, "iota should continue to increment, array to array.")
45
+ end
46
+ end
47
+
48
+ class Iota_Block < Test::Unit::TestCase
49
+ set_iota 1
50
+ iota(%w(KB MB GB TB)) do |x|
51
+ 1 << x*10
52
+ end
53
+
54
+ def test_block
55
+ assert_equal( 1024, KB, "iota should increment in blocks.")
56
+ assert_equal( 1048576, MB, "iota should increment in blocks.")
57
+ assert_equal( 1073741824, GB, "iota should increment in blocks.")
58
+ assert_equal(1099511627776, TB, "iota should increment in blocks.")
59
+ end
60
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: iota
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Sam Rawlins
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-11 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: iota - emulating Go's iota... thing.
17
+ email:
18
+ - sam.rawlins@gmail.com
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - lib/iota.rb
27
+ - test/tests.rb
28
+ - README.rdoc
29
+ has_rdoc: false
30
+ homepage: http://github.com/srawlins/iota
31
+ licenses: []
32
+
33
+ post_install_message:
34
+ rdoc_options: []
35
+
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 1.8.1
43
+ version:
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ requirements: []
51
+
52
+ rubyforge_project:
53
+ rubygems_version: 1.3.5
54
+ signing_key:
55
+ specification_version: 3
56
+ summary: Emulates Go's iota... thing.
57
+ test_files: []
58
+