Skip to content

Instantly share code, notes, and snippets.

@ms-tg
Forked from rondale-sc/gist-1.rb
Created October 18, 2012 18:13

Revisions

  1. ms-tg revised this gist Oct 18, 2012. 2 changed files with 35 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion gist-5.rb
    Original file line number Diff line number Diff line change
    @@ -19,8 +19,10 @@ def display_name
    end
    end

    ## Expose the option to user of class
    ## Exposing the option to user of class
    puts Project.new.supervisor
    # => None
    puts Project.new(Supervisor.new("Arthur", "Dent")).map(&:display_name).get_or_else("No supervisor assigned.")
    # => Arthur, Dent
    puts Project.new.supervisor.map(&:display_name).get_or_else("No supervisor assigned.")
    # => No supervisor assigned.
    32 changes: 32 additions & 0 deletions gist-6.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    require 'rubygems'
    require 'rumonade'
    include Rumonade

    class Project
    attr_reader :supervisor

    # assume supervisor looks like:
    # Supervisor.new("Harry", "Henderson", "hhendersonfake@me.com")

    def initialize(supervisor=nil)
    @supervisor = supervisor
    end

    def supervisor_display_name
    Option(supervisor).map(&:display_name).get_or_else("No supervisor assigned.")
    end
    end

    Supervisor = Struct.new(:first_name, :last_name, :email) do
    def display_name
    first_name + ", " + last_name
    end
    end

    ## Option used internally, not exposed to user of class
    puts Project.new.supervisor
    # =>
    puts Project.new(Supervisor.new("Arthur", "Dent")).supervisor_display_name
    # => Arthur, Dent
    puts Project.new.supervisor_display_name
    # => No supervisor assigned.
  2. ms-tg revised this gist Oct 18, 2012. 1 changed file with 26 additions and 0 deletions.
    26 changes: 26 additions & 0 deletions gist-5.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    require 'rubygems'
    require 'rumonade'
    include Rumonade

    class Project
    attr_reader :supervisor # this is now an Option of Supervisor (this is a "monad")

    # assume supervisor looks like:
    # Supervisor.new("Harry", "Henderson", "hhendersonfake@me.com")

    def initialize(supervisor=None)
    @supervisor = supervisor
    end
    end

    Supervisor = Struct.new(:first_name, :last_name, :email) do
    def display_name
    first_name + ", " + last_name
    end
    end

    ## Expose the option to user of class
    puts Project.new.supervisor
    # => None
    puts Project.new.supervisor.map(&:display_name).get_or_else("No supervisor assigned.")
    # => No supervisor assigned.
  3. @rondale-sc rondale-sc revised this gist Oct 16, 2012. 2 changed files with 16 additions and 16 deletions.
    10 changes: 5 additions & 5 deletions gist-2.rb
    Original file line number Diff line number Diff line change
    @@ -2,19 +2,19 @@ class Project
    attr_reader :supervisor

    # assume supervisor looks like:
    # ["Harry", "Henderson", "hhendersonfake@me.com"]
    # Supervisor.new("Harry", "Henderson", "hhendersonfake@me.com")

    def initialize(supervisor)
    @supervisor = Supervisor.new(*supervisor)
    @supervisor = supervisor
    end
    end

    Supervisor = Struct.new(:first_name, :last_name, :email) do
    Supervisor = Struct.new(:first_name, :last_name, :email) do
    def display_name
    if !first_name.nil? || !last_name.nil?
    first_name + ", " + last_name
    else
    "No supervisor assigned."
    end
    end
    end
    end
    end
    22 changes: 11 additions & 11 deletions gist-4.rb
    Original file line number Diff line number Diff line change
    @@ -2,21 +2,21 @@ class Project
    attr_reader :supervisor

    # assume supervisor looks like:
    # ["Harry", "Henderson", "hhendersonfake@me.com"]
    # Supervisor.new("Harry", "Henderson", "hhendersonfake@me.com")

    def initialize(supervisor=nil)
    @supervisor = supervisor.nil? ? NullSupervisor.new : Supervisor.new(*supervisor)
    def initialize(supervisor=NullSupervisor.new)
    @supervisor = supervisor
    end
    end

    Supervisor = Struct.new(:first_name, :last_name, :email) do
    def display_name
    first_name + ", " + last_name
    end
    Supervisor = Struct.new(:first_name, :last_name, :email) do
    def display_name
    first_name + ", " + last_name
    end
    end

    NullSupervisor = Struct.new() do
    def display_name
    "No supervisor assigned."
    end
    NullSupervisor = Struct.new() do
    def display_name
    "No supervisor assigned."
    end
    end
  4. @rondale-sc rondale-sc revised this gist Oct 16, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gist-1.rb
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ def initialize(supervisor)
    end

    def display_name
    if !supervisor
    if supervisor
    supervisor[0] + ", " + supervisor[1] # yuck
    else
    "No supervisor assigned."
  5. @rondale-sc rondale-sc created this gist Oct 16, 2012.
    18 changes: 18 additions & 0 deletions gist-1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    class Project
    attr_reader :supervisor

    # assume supervisor looks like:
    # ["Harry", "Henderson", "hhendersonfake@me.com"]

    def initialize(supervisor)
    @supervisor = supervisor
    end

    def display_name
    if !supervisor
    supervisor[0] + ", " + supervisor[1] # yuck
    else
    "No supervisor assigned."
    end
    end
    end
    20 changes: 20 additions & 0 deletions gist-2.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    class Project
    attr_reader :supervisor

    # assume supervisor looks like:
    # ["Harry", "Henderson", "hhendersonfake@me.com"]

    def initialize(supervisor)
    @supervisor = Supervisor.new(*supervisor)
    end

    Supervisor = Struct.new(:first_name, :last_name, :email) do
    def display_name
    if !first_name.nil? || !last_name.nil?
    first_name + ", " + last_name
    else
    "No supervisor assigned."
    end
    end
    end
    end
    5 changes: 5 additions & 0 deletions gist-3.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    NullSupervisor = Struct.new() do
    def display_name
    "No supervisor assigned."
    end
    end
    22 changes: 22 additions & 0 deletions gist-4.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    class Project
    attr_reader :supervisor

    # assume supervisor looks like:
    # ["Harry", "Henderson", "hhendersonfake@me.com"]

    def initialize(supervisor=nil)
    @supervisor = supervisor.nil? ? NullSupervisor.new : Supervisor.new(*supervisor)
    end

    Supervisor = Struct.new(:first_name, :last_name, :email) do
    def display_name
    first_name + ", " + last_name
    end
    end

    NullSupervisor = Struct.new() do
    def display_name
    "No supervisor assigned."
    end
    end
    end