class RubyLisp::Symbol

Public Instance Methods

==(other) click to toggle source
# File lib/rubylisp/types.rb, line 80
def ==(other)
  other.is_a?(Symbol) && @value == other.value
end
resolve(env) click to toggle source
# File lib/rubylisp/types.rb, line 84
def resolve(env)
  # rbl: (.+ 1 2)
  # ruby: 1.+(2)
  instance_method = /^\.(.*)/.match(@value).to_a[1]
  if instance_method
    return lambda {|obj, *args| obj.send instance_method, *args}
  end

  # rbl: (File::open "/tmp/foo")
  # ruby: File::open("/tmp/foo")  OR  File.open("/tmp/foo")
  #
  # rbl: Foo::Bar::BAZ
  # ruby: Foo::Bar::BAZ
  if /^\w+(::\w+)+$/ =~ @value
    first_segment, *segments = @value.split('::')
    first_segment = Object.const_get first_segment
    return segments.reduce(first_segment) do |result, segment|
      if result.is_a? Proc
        # a method can only be in the final position
        raise RuntimeError, "Invalid value: #{@value}"
      elsif /^[A-Z]/ =~ segment
        # get module/class constant
        result.const_get segment
      else
        # if module/class method doesn't exist, trigger a NoMethodError
        result.send segment unless result.respond_to? segment
        # call module/class method
        lambda {|*args| result.send segment, *args }
      end
    end
  end

  env.get @value
end
to_s() click to toggle source
# File lib/rubylisp/types.rb, line 76
def to_s
  @value
end