keycounter 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/examples/countries.rb +39 -0
  3. data/lib/keycounter.rb +44 -0
  4. metadata +45 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1992f04111944645f80ea3cb5743fac9530dff5c
4
+ data.tar.gz: 8eea1926b2d4f514a80f78736cc3fe9ecefcebf4
5
+ SHA512:
6
+ metadata.gz: 9f77e8a90a52bec0cec5aaaa3da542a0f0b191c12238e78d643436fdfde28560020dadfd808759a22d21ea4b138dffe2a3bbc6f4eb3fa48a3536da2c6c4eda6b
7
+ data.tar.gz: c9d82b956036ba33bd3a3d31a609c5714ce9ba2a98c467e4506e8dd3904ab4ee3639f445525924156284c4b6bbe5fb4d045b874f44b3e534b91ed3f4be82b82a
@@ -0,0 +1,39 @@
1
+ ########################################################################
2
+ #
3
+ # Author: Brian Hood
4
+ #
5
+ # Description: Keycounter
6
+ #
7
+ # Example data
8
+ #
9
+ ########################################################################
10
+
11
+
12
+ require File.expand_path(File.join(
13
+ File.dirname(__FILE__),
14
+ "../lib/keycounter.rb"))
15
+
16
+ k = Keycounter.new
17
+ # Will add / make USA a value of 3
18
+ k.keycount("USA")
19
+ k.keycount("USA")
20
+ k.keycount("USA")
21
+ # Will add / make UK 4
22
+ k.keycount("UK")
23
+ k.keycount("UK")
24
+ k.keycount("UK")
25
+ k.keycount("UK")
26
+ k.keycount("UK")
27
+
28
+ # With a loop
29
+
30
+ 3.times {|n|
31
+ k.keycount("France")
32
+ }
33
+
34
+ # Read a keys count
35
+ k.keycount_reader("USA")
36
+
37
+ # Display all values
38
+ k.keycount_compile
39
+
@@ -0,0 +1,44 @@
1
+ ########################################################################
2
+ #
3
+ # Author: Brian Hood
4
+ #
5
+ # Description: Keycounter
6
+ #
7
+ # A Simple counter using just instance variables
8
+ #
9
+ ########################################################################
10
+
11
+ class Keycounter
12
+
13
+ # Create / Add to instance variable
14
+ def keycount(key)
15
+ key.gsub!(" ", "_") # no spaces or case
16
+ if !instance_variable_get("@#{key}")
17
+ instance_variable_set("@#{key}", 1)
18
+ else
19
+ instance_variable_set("@#{key}", instance_variable_get("@#{key}") + 1)
20
+ end
21
+ puts "Key: #{key} Value: "+instance_variable_get("@#{key}").to_s
22
+ end
23
+
24
+ # Read a single key
25
+ def keycount_reader(key)
26
+ key.gsub!(" ", "_") # no spaces or case
27
+ valnum = instance_variable_get("@#{key}")
28
+ return valnum
29
+ end
30
+
31
+ # Compile in array with the totals of all instance variables
32
+ def keycount_compile
33
+ keycounts = Array.new
34
+ # You can't really inherit this class as the other class may also contain instance variables
35
+ # its not really an exact logic this class only works alone.
36
+ instance_variables.each {|n|
37
+ t = n.to_s.gsub("@", "")
38
+ keycounts << ["#{t}", instance_variable_get("#{n}")]
39
+ }
40
+ return keycounts
41
+ keycounts
42
+ end
43
+
44
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: keycounter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Brian Hood
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-24 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Count up value totals
14
+ email: brianh6854@googlemail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - examples/countries.rb
20
+ - lib/keycounter.rb
21
+ homepage: http://rubygems.org/gems/keycounter
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.2.2
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Key value counter
45
+ test_files: []