Module: LiveAST::Common

Included in:
Evaler
Defined in:
lib/live_ast/common.rb

Class Method Summary collapse

Class Method Details

.arg_to_str(arg) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/live_ast/common.rb', line 6

def arg_to_str(arg)
  begin
    arg.to_str
  rescue NameError
    thing = if arg.nil? then nil else arg.class end

    raise TypeError, "can't convert #{thing.inspect} into String"
  end
end

.check_arity(args, range) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/live_ast/common.rb', line 16

def check_arity(args, range)
  unless range.include? args.size
    range = 0 if range == (0..0)

    raise ArgumentError,
    "wrong number of arguments (#{args.size} for #{range})"
  end
end

.check_type(obj, klass) ⇒ Object



25
26
27
28
29
# File 'lib/live_ast/common.rb', line 25

def check_type(obj, klass)
  unless obj.is_a? klass
    raise TypeError, "wrong argument type #{obj.class} (expected #{klass})"
  end
end

.location_for_eval(*args) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/live_ast/common.rb', line 31

def location_for_eval(*args)
  bind, *location = args

  if bind
    case location.size
    when 0
      NATIVE_EVAL.call("[__FILE__, __LINE__]", bind)
    when 1
      [location.first, 1]
    else
      location
    end
  else
    ["(eval)", 1]
  end
end