-
-
Save ms-tg/3913860 to your computer and use it in GitHub Desktop.
try_and_try_again
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Project | |
attr_reader :supervisor | |
# assume supervisor looks like: | |
# ["Harry", "Henderson", "[email protected]"] | |
def initialize(supervisor) | |
@supervisor = supervisor | |
end | |
def display_name | |
if supervisor | |
supervisor[0] + ", " + supervisor[1] # yuck | |
else | |
"No supervisor assigned." | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Project | |
attr_reader :supervisor | |
# assume supervisor looks like: | |
# Supervisor.new("Harry", "Henderson", "[email protected]") | |
def initialize(supervisor) | |
@supervisor = supervisor | |
end | |
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NullSupervisor = Struct.new() do | |
def display_name | |
"No supervisor assigned." | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Project | |
attr_reader :supervisor | |
# assume supervisor looks like: | |
# Supervisor.new("Harry", "Henderson", "[email protected]") | |
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 | |
end | |
NullSupervisor = Struct.new() do | |
def display_name | |
"No supervisor assigned." | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", "[email protected]") | |
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 | |
## 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'rumonade' | |
include Rumonade | |
class Project | |
attr_reader :supervisor | |
# assume supervisor looks like: | |
# Supervisor.new("Harry", "Henderson", "[email protected]") | |
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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment