Ruby: Specify language of code snippets
@@ -58,6 +58,7 @@ in the Ruby-bindings source tree under 'examples'. Also the test cases (under
This is a short example and illustrates how to use the binding:
+```ruby
require 'kdb'
Kdb.open do |db|
@@ -74,6 +75,7 @@ This is a short example and illustrates how to use the binding:
db.set ks, '/'
end
+```
## Get a KDB handle
@@ -81,20 +83,26 @@ There are two ways to create a KDB handle:
Creating a new KDB instance:
+```ruby
db = Kdb::KDB.new
# requires a db.close afterwards
+```
or with the `open` method:
+```ruby
db = Kdb.open
# requires a db.close afterwards
+```
This `open` method supports also a block:
+```ruby
Kdb.open do |db|
...
end
# db.close is called implecitly
+```
Note: after the block was executed, `db.close` is called implicitly, Thus you
can not use the handle afterwards.
@@ -103,6 +111,7 @@ can not use the handle afterwards.
Exception handling is directly mapped from the C++ binding:
+```ruby
begin
db = Kdb.open
...
@@ -111,6 +120,7 @@ Exception handling is directly mapped from the C++ binding:
ensure
db.close
end
+```
## KeySet creation
@@ -118,50 +128,66 @@ A KeySet can be created in different ways:
An empty key set:
+```ruby
ks = Kdb::KeySet.new
+```
A key set with one initial key:
+```ruby
ks = Kdb::KeySet.new Kdb::Key.new("user/sw/key1")
+```
Passing an array of initial keys:
+```ruby
ks = Kdb::KeySet.new [
Kdb::Key.new "user/sw/key1"
Kdb::Key.new "user/sw/key2"
Kdb::Key.new "user/sw/key3"
]
+```
Passing a KeySet of initial keys:
+```ruby
ks2 = Kdb::KeySet.new ks
+```
## Appending keys
`KeySet` supports Ruby's "append" operator `<<`
+```ruby
ks << Kdb::Key.new("user/sw/key1")
+```
## iteration
`KeySet` has an `each` method and includes `Enumerable`
+```ruby
ks.each { |k| puts k.to_s }
ks.any? { |k| k.has_meta? "owner" }
+```
## Deleting Keys
Similar to the Ruby `Array`, `KeySet` also implements `delete` and `delete_at`
methods, which can be used to delete a key by name or by index:
+```ruby
ks.delete "user/sw/key1"
ks.delete_at 2
+```
## Key creation
Keys can be created with a Hash-like variable argument list:
+```ruby
Kdb::Key.new "user/sw/key1", value: "v1", owner: "me"
+```
The first argument is the name of the `Key`, followed by an optional list of
symbol value pairs. The symbol value pairs are interpreted as follows:
@@ -175,9 +201,11 @@ symbol value pairs. The symbol value pairs are interpreted as follows:
This is very similar to the `KeySet` iteration and can be accessed with `meta`:
+```ruby
k.meta.each do |m|
puts "#{m.name}: #{m.value}"
end
+```
## Building
