Class: Bridge::Bid

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/bridge/bid.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bid) ⇒ Bid

Creates a new bid

Raises:

  • (ArgumentError)


8
9
10
11
# File 'lib/bridge/bid.rb', line 8

def initialize(bid)
  @bid = bid.to_s.upcase
  raise ArgumentError, "invalid bid: #{bid}" unless Bridge.bid?(@bid)
end

Instance Attribute Details

#bidObject (readonly)

Returns the value of attribute bid.



5
6
7
# File 'lib/bridge/bid.rb', line 5

def bid
  @bid
end

Instance Method Details

#<=>(other) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/bridge/bid.rb', line 68

def <=>(other)
  case other
  when Bid
    if contract?
      raise ArgumentError, "could not compare contract bid with non-contract bid #{other}" unless other.contract?
      Bridge.compare_contracts(self.bid, other.bid)
    elsif pass?
      raise ArgumentError, "could not compare pass bid with non-pass bid #{other}" unless other.pass?
      bid <=> other.bid
    elsif double?
      raise ArgumentError, "could not compare double bid with non-double bid #{other}" unless other.double?
      bid <=> other.bid
    elsif redouble?
      raise ArgumentError, "could not compare redouble bid with non-redouble bid #{other}" unless other.redouble?
      bid <=> other.bid
    end
  when String
    self <=> Bid.new(other)
  else
    begin
      a, b = other.coerce(self)
      a <=> b
    rescue
    end
  end
end

#coerce(other) ⇒ Object



95
96
97
# File 'lib/bridge/bid.rb', line 95

def coerce(other)
  [Bid.new(other.to_s), self]
end

#contract?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/bridge/bid.rb', line 64

def contract?
  Bridge.contract?(bid)
end

#double?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/bridge/bid.rb', line 44

def double?
  Bridge.double?(bid)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/bridge/bid.rb', line 99

def eql?(other)
  self == other && other.instance_of?(Bid)
end

#grand_slam?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/bridge/bid.rb', line 60

def grand_slam?
  level.to_i == 7
end

#hashObject



103
104
105
# File 'lib/bridge/bid.rb', line 103

def hash
  bid.hash
end

#inspectObject



111
112
113
# File 'lib/bridge/bid.rb', line 111

def inspect
  bid.inspect
end

#levelObject

Returns the level of the bid



14
15
16
# File 'lib/bridge/bid.rb', line 14

def level
  bid[0] if contract?
end

#major?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/bridge/bid.rb', line 31

def major?
  Bridge.major?(suit)
end

#minor?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/bridge/bid.rb', line 27

def minor?
  Bridge.minor?(suit)
end

#modifier?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/bridge/bid.rb', line 52

def modifier?
  Bridge.modifier?(bid)
end

#no_trump?Boolean Also known as: nt?

Returns:

  • (Boolean)


35
36
37
# File 'lib/bridge/bid.rb', line 35

def no_trump?
  Bridge.no_trump?(suit)
end

#pass?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/bridge/bid.rb', line 40

def pass?
  Bridge.pass?(bid)
end

#redouble?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/bridge/bid.rb', line 48

def redouble?
  Bridge.redouble?(bid)
end

#small_slam?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/bridge/bid.rb', line 56

def small_slam?
  level.to_i == 6
end

#suitObject

Returns the suit of the bid



19
20
21
# File 'lib/bridge/bid.rb', line 19

def suit
  bid[1..-1] if contract?
end

#to_sObject



107
108
109
# File 'lib/bridge/bid.rb', line 107

def to_s
  bid
end

#trumpObject



23
24
25
# File 'lib/bridge/bid.rb', line 23

def trump
  suit if Bridge.trump?(suit)
end