Skip to content

Instantly share code, notes, and snippets.

@havenwood
Last active March 11, 2022 17:51
Show Gist options
  • Select an option

  • Save havenwood/a81401724cbcbf1f78e79f42ed4b0e33 to your computer and use it in GitHub Desktop.

Select an option

Save havenwood/a81401724cbcbf1f78e79f42ed4b0e33 to your computer and use it in GitHub Desktop.

Revisions

  1. havenwood revised this gist Mar 11, 2022. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions multiple_instances_of_state.rb
    Original file line number Diff line number Diff line change
    @@ -14,6 +14,9 @@ def foo
    an_instance.foo
    #=> "tabmow"

    another_instance = MultipleInstancesOfState.new(state: 'tabmow')
    another_instance.foo
    an_instance.foo
    #=> "wombat"

    another_instance = MultipleInstancesOfState.new(state: 'corge')
    another_instance.foo
    #=> "egroc"
  2. havenwood revised this gist Feb 24, 2022. 3 changed files with 7 additions and 7 deletions.
    4 changes: 2 additions & 2 deletions multiple_instances_of_state.rb
    Original file line number Diff line number Diff line change
    @@ -14,6 +14,6 @@ def foo
    an_instance.foo
    #=> "tabmow"

    another_instance = MultipleInstancesOfState.new(state: 'wombat')
    another_instance = MultipleInstancesOfState.new(state: 'tabmow')
    another_instance.foo
    #=> "tabmow"
    #=> "wombat"
    8 changes: 4 additions & 4 deletions no_state.rb
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,13 @@
    module NoState
    module_function

    def foo(bar:)
    bar.reverse
    def foo(stateless:)
    stateless.reverse
    end
    end

    NoState.foo(bar: 'wombat')
    NoState.foo(stateless: 'wombat')
    #=> "tabmow"

    NoState.foo(bar: 'tabmow')
    NoState.foo(stateless: 'tabmow')
    #=> "wombat"
    2 changes: 1 addition & 1 deletion single_instance_of_state.rb
    Original file line number Diff line number Diff line change
    @@ -18,5 +18,5 @@ def foo
    singleton_instance.state = 'wombat'
    singleton_instance.foo
    #=> "tabmow"
    SingleInstanceOfState.instance.foo
    singleton_instance.foo
    #=> "wombat"
  3. havenwood revised this gist Feb 21, 2020. 2 changed files with 5 additions and 1 deletion.
    4 changes: 4 additions & 0 deletions no_state.rb
    Original file line number Diff line number Diff line change
    @@ -7,3 +7,7 @@ def foo(bar:)
    end

    NoState.foo(bar: 'wombat')
    #=> "tabmow"

    NoState.foo(bar: 'tabmow')
    #=> "wombat"
    2 changes: 1 addition & 1 deletion single_instance_of_state.rb
    Original file line number Diff line number Diff line change
    @@ -18,5 +18,5 @@ def foo
    singleton_instance.state = 'wombat'
    singleton_instance.foo
    #=> "tabmow"
    singleton_instance.foo
    SingleInstanceOfState.instance.foo
    #=> "wombat"
  4. havenwood created this gist Feb 20, 2020.
    19 changes: 19 additions & 0 deletions multiple_instances_of_state.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    class MultipleInstancesOfState
    attr_accessor :state

    def initialize(state:)
    @state = state
    end

    def foo
    @state.reverse!
    end
    end

    an_instance = MultipleInstancesOfState.new(state: 'wombat')
    an_instance.foo
    #=> "tabmow"

    another_instance = MultipleInstancesOfState.new(state: 'wombat')
    another_instance.foo
    #=> "tabmow"
    9 changes: 9 additions & 0 deletions no_state.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    module NoState
    module_function

    def foo(bar:)
    bar.reverse
    end
    end

    NoState.foo(bar: 'wombat')
    22 changes: 22 additions & 0 deletions single_instance_of_state.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    require 'singleton'

    class SingleInstanceOfState
    include Singleton

    attr_accessor :state

    def initialize
    @state = nil
    end

    def foo
    @state.reverse!
    end
    end

    singleton_instance = SingleInstanceOfState.instance
    singleton_instance.state = 'wombat'
    singleton_instance.foo
    #=> "tabmow"
    singleton_instance.foo
    #=> "wombat"