Skip to content

class Crystal::Macros::MacroId
inherits Crystal::Macros::ASTNode

A fictitious node representing an identifier like, foo, Bar or something_else.

The parser doesn't create these nodes. Instead, you create them by invoking id on some nodes. For example, invoking id on a StringLiteral returns a MacroId for the string's content. Similarly, invoking ID on a SymbolLiteral, Call, Var and Path returns a MacroId for the node's content.

This allows you to treat strings, symbols, variables and calls uniformly. For example:

macro getter(name)
  def {{name.id}}
    @{{name.id}}
  end
end

getter unicorns
getter :unicorns
getter "unicorns"

All of the above macro calls work because we invoked id, and the generated code looks like this:

def unicorns
  @unicorns
end

If we hadn't used id, the generated code would have been this:

def unicorns
  @unicorns
end

def :unicorns
  @:unicorns
end

def "unicorns"
  @"unicorns"
end

The last two definitions are invalid and so will give a compile-time error.

Methods

#+(other : StringLiteral | CharLiteral) : MacroId

Similar to String#+.

View source

#=~(range : RegexLiteral) : BoolLiteral

Similar to String#=~.

View source

#[](range : RangeLiteral) : MacroId

Similar to String#[].

View source

#capitalize : MacroId

Similar to String#capitalize.

View source

#chomp : MacroId

Similar to String#chomp.

View source

#downcase : MacroId

Similar to String#downcase.

View source

#empty? : BoolLiteral

Similar to String#empty?.

View source

#ends_with?(other : StringLiteral | CharLiteral) : BoolLiteral

Similar to String#ends_with?.

View source

#gsub(regex : RegexLiteral, replacement : StringLiteral) : MacroId

Similar to String#gsub.

View source

#id : MacroId

Returns self.

View source

#includes?(search : StringLiteral | CharLiteral) : BoolLiteral

Similar to String#includes?.

View source

#size : NumberLiteral

Similar to String#size.

View source

#starts_with?(other : StringLiteral | CharLiteral) : BoolLiteral

Similar to String#starts_with?.

View source

#strip : MacroId

Similar to String#strip.

View source

#titleize : MacroId

Similar to String#titleize.

View source

#tr(from : StringLiteral, to : StringLiteral) : MacroId

Similar to String#tr.

View source

#upcase : MacroId

Similar to String#upcase.

View source