Class: Just

Inherits:
Object
  • Object
show all
Includes:
Contracts::Core, Maybe
Defined in:
lib/maybe.rb

Overview

Just is a member of the Maybe union type that represents a non-null value. It’s used in conjunction with the Nothing type to allow one to gracefully handle null values without having to create a large amount of conditional logic.

Instance Method Summary collapse

Methods included from Maybe

Just, Nothing, #Proc, from_nullable, lift, of, zip

Constructor Details

#initialize(value) ⇒ Just

Returns a new instance of Just.


329
330
331
# File 'lib/maybe.rb', line 329

def initialize(value)
  @value = value
end

Instance Method Details

#==(m) ⇒ Object


444
445
446
# File 'lib/maybe.rb', line 444

def ==(m)
  m.is_a?(Just) && m.get == get
end

#ap(m) ⇒ Object Also known as: apply


427
428
429
# File 'lib/maybe.rb', line 427

def ap(m)
  m.flat_map {|f| map(&f) }
end

#flat_map(&f) ⇒ Object


414
415
416
# File 'lib/maybe.rb', line 414

def flat_map(&f)
  f.call(@value)
end

#getObject


343
344
345
# File 'lib/maybe.rb', line 343

def get
  @value
end

#get_or_else(&f) ⇒ Object


357
358
359
# File 'lib/maybe.rb', line 357

def get_or_else(&f)
  @value
end

#inspectObject


456
457
458
# File 'lib/maybe.rb', line 456

def inspect
  "Just(#{@value.inspect})"
end

#just?Boolean

Returns:

  • (Boolean)

365
366
367
# File 'lib/maybe.rb', line 365

def just?
  true
end

#map(&f) ⇒ Object


391
392
393
# File 'lib/maybe.rb', line 391

def map(&f)
  flat_map {|value| Maybe.of(f.call(value)) }
end

#nothing?Boolean

Returns:

  • (Boolean)

373
374
375
# File 'lib/maybe.rb', line 373

def nothing?
  false
end