From 87bdda3c9acd696a871bc581825672a8aa37b14d Mon Sep 17 00:00:00 2001 From: jimtng <2554958+jimtng@users.noreply.github.com> Date: Fri, 25 Aug 2023 22:55:15 +1000 Subject: [PATCH] Minor corrections in documentation on timers and shared_cache (#118) --- USAGE.md | 16 ++++++++-------- lib/openhab/core/value_cache.rb | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/USAGE.md b/USAGE.md index 17b9165a1e..af0a8dab4d 100644 --- a/USAGE.md +++ b/USAGE.md @@ -850,7 +850,7 @@ It can be rescheduled to a different duration ```ruby after(3.minutes) do |timer| My_Light.on - my_timer.reschedule(1.minute) + timer.reschedule(1.minute) end ``` @@ -922,22 +922,22 @@ Just be wary of Ruby-only data types (such as Symbols) that won't be accessible Get a previously set object with a default value: ```ruby -counter_object = shared_cache.compute_if_absent(:counter) { { "times" => 0 } } -logger.info("Count: #{counter_object["times"] += 1}")" +shared_cache.compute_if_absent(:counter) { 0 } # Initialize with 0 if it didn't exist +logger.info("Count: #{shared_cache[:counter] += 1}") ``` Get a previously set object, or assign it (this version is subject to race conditions with other scripts): ```ruby -counter_object = shared_cache[:counter] ||= { "times" => 0 } -logger.info("Count: #{counter_object["times"] += 1}")" +shared_cache[:counter] ||= 0 +logger.info("Count: #{shared_cache[:counter] += 1}") ``` -Get a previously set object with a default value, without assigning it (this version has an even longer amount of time between fetchig the value and assigning it): +Get a previously set object with a default value, without assigning it (this version has an even longer amount of time between fetching the value and assigning it): ```ruby -count = shared_count.fetch(:counter) { 0 } -shared_count[:counter] = count + 1 +count = shared_cache.fetch(:counter) { 0 } +shared_cache[:counter] = count + 1 ``` ### Time diff --git a/lib/openhab/core/value_cache.rb b/lib/openhab/core/value_cache.rb index e8fd1d63a3..2e26d79c43 100644 --- a/lib/openhab/core/value_cache.rb +++ b/lib/openhab/core/value_cache.rb @@ -28,7 +28,7 @@ module Core # For a private cache, simply use an instance variable. See # {file:docs/ruby-basics.md#variables Instance Variables}. # - # @note Because every script or UI rule gets it own JRuby engine instance, + # @note Because every script or UI rule gets its own JRuby engine instance, # you cannot rely on being able to access Ruby objects between them. Only # objects that implement a Java interface that's part of Java or openHAB # Core (such as Hash implements {java.util.Map}, or other basic