~ubuntu-branches/ubuntu/intrepid/racc/intrepid

« back to all changes in this revision

Viewing changes to packages/amstd/lib/amstd/must.rb

  • Committer: Bazaar Package Importer
  • Author(s): akira yamada
  • Date: 2005-04-09 17:54:44 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20050409175444-fvtir838ypkn7a58
Tags: 1.4.4-1
* new upstream version.  (closes: #301768)
* added racc2y.1 and y2racc.1.
* modified racc2y and y2racc to use optparse instead of deprecated getopts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#
2
 
# must.rb
3
 
#
4
 
#   Copyright (c) 1999,2000 Minero Aoki <aamine@loveruby.net>
5
 
#
6
 
#   This program is free software.
7
 
#   You can distribute/modify this program under the terms of
8
 
#   the GNU General Public License version 2 or later.
9
 
#
10
 
 
11
 
require 'amstd/to_s'
12
 
 
13
 
 
14
 
class Object
15
 
 
16
 
  def must( *args )
17
 
    args.each {|c| return self if c === self }
18
 
    raise TypeError, "wrong arg type '#{type}' for required #{args.join('/')}"
19
 
  end
20
 
 
21
 
  def must_have( *args )
22
 
    args.each do |m|
23
 
      unless self.respond_to? m then
24
 
        name = _name2str( m )
25
 
        tos = self.to_s; tos = tos[0, 15] + '...' if tos.size > 15
26
 
        raise ArgumentError, "arg #{tos}(#{type}) does not have '#{name}'"
27
 
      end
28
 
    end
29
 
    self
30
 
  end
31
 
  alias needed must_have
32
 
 
33
 
  def must_be( eq )
34
 
    unless self == eq then
35
 
      raise ArgumentError, "expected #{eq.inspect} but is #{inspect}(#{type})"
36
 
    end
37
 
    self
38
 
  end
39
 
 
40
 
  def must_exist
41
 
    if self.nil? then
42
 
      raise ArgumentError, 'is wrongly nil'
43
 
    end
44
 
  end
45
 
 
46
 
end