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



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

def arg_to_str(arg)
  arg.to_str
rescue NameError
  thing = arg.nil? ? nil : arg.class

  message = "no implicit conversion of #{thing.inspect} into String"
  raise TypeError, message
end

.arg_to_str2(arg) ⇒ Object



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

def arg_to_str2(arg)
  arg.to_str
rescue NameError
  thing = arg.nil? ? nil : arg.class

  message = "wrong argument type #{thing.inspect} (expected String)"
  raise TypeError, message
end

.check_arity(args, range) ⇒ Object

Raises:

  • (ArgumentError)


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

def check_arity(args, range)
  return if range.include? args.size

  range = 0 if range == (0..0)

  message = "wrong number of arguments (given #{args.size}, expected #{range})"
  raise ArgumentError, message
end

.check_is_binding(obj) ⇒ Object

Raises:

  • (TypeError)


34
35
36
37
38
39
# File 'lib/live_ast/common.rb', line 34

def check_is_binding(obj)
  return if obj.is_a? Binding

  message = "wrong argument type #{obj.class} (expected binding)"
  raise TypeError, message
end

.location_for_eval(*args) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/live_ast/common.rb', line 41

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

  if bind
    case location.size
    when 0
      bind.source_location
    when 1
      [location.first, 1]
    else
      location
    end
  else
    ["(eval)", 1]
  end
end