File: 06_open_cascade.rdoc

package info (click to toggle)
ruby-hashery 2.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 404 kB
  • sloc: ruby: 2,997; makefile: 7
file content (64 lines) | stat: -rw-r--r-- 1,524 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
= OpenCascade

The reason this class is labeled "cascade", is that every internal
Hash is transformed into an OpenCascade dynamically upon access.
This makes it easy to create _cascading_ references.

    h = { :x => { :y => { :z => 1 } } }
    c = OpenCascade[h]
    assert c.x.y.z == 1

As soon as you access a node it automatically becomes an OpenCascade.

    c = OpenCascade.new
    assert(OpenCascade === c.r)
    assert(OpenCascade === c.a.b)

But if you set a node, then that will be it's value.

    c.a.b = 4
    assert c.a.b == 4

To query a node without causing the auto-creation of an OpenCasade
object, use the ?-mark.

   assert c.a.z? == nil

OpenCascade also transforms Hashes within Arrays.

   h = { :x=>[ {:a=>1}, {:a=>2} ], :y=>1 }
   c = OpenCascade[h]
   assert c.x.first.a == 1
   assert c.x.last.a == 2

Like OpenObject, OpenCascade allows you to insert entries as array
pairs.

   c = OpenCascade.new
   c << [:x,8]
   c << [:y,9]

   assert c.x == 8
   assert c.y == 9

Finally, you can call methods ending in a !-mark to access the
underlying hash (Note that these differ in behavior from the
built-in !-methods).

   bk = c.map!{ |k,v| k.to_s.upcase }
   bk.sort.assert == ['X', 'Y']

So you can see that for the most an OpenCascade is just like
OpenHash, but it allows us to conveniently build open sub-layers
easily.

Enumerable still works with OpenCascades too.

    h = {}
    c = OpenCascade[:a=>1,:b=>{:c=>3}]
    c.each do |k,v|
      h[k] = v
    end
    OpenCascade.assert === h[:b]