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&.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
24
25
26
27
28
29
30
# File 'lib/live_ast/common.rb', line 16

def arg_to_str2(arg)
  return "" if arg.nil? && RUBY_VERSION >= "3.3.0"

  arg.to_str
rescue NameError
  thing = arg&.class

  message = if arg.nil?
              "wrong argument type #{thing.inspect} (expected String)"
            else
              "no implicit conversion of #{thing.inspect} into String"
            end

  raise TypeError, message
end

.check_arity(args, range) ⇒ Object

Raises:

  • (ArgumentError)


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

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)


41
42
43
44
45
46
# File 'lib/live_ast/common.rb', line 41

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(bind, filename = nil, lineno = nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/live_ast/common.rb', line 48

def location_for_eval(bind, filename = nil, lineno = nil)
  if filename
    lineno ||= 1
    [filename, lineno]
  elsif RUBY_VERSION >= "3.3.0"
    file, line = bind.source_location
    ["(eval at #{file}:#{line})", 1]
  else
    ["(eval)", 1]
  end
end