Skip to content

Instantly share code, notes, and snippets.

@qnighy
Last active January 3, 2025 08:33
Show Gist options
  • Save qnighy/55b3fb0b2f60876487f011c80a4c3270 to your computer and use it in GitHub Desktop.
Save qnighy/55b3fb0b2f60876487f011c80a4c3270 to your computer and use it in GitHub Desktop.
Ruby syntaxes

Program-global

  • # frozen_string_literal
  • # encoding
  • __END__

Expressions

  • Singleton literals
    • nil
    • true
    • false
  • Numeric literals
    • 123
    • 123.0
    • 123r
    • 123i
  • String-likes
    • /re/ (w/ or w/o interpolation)
    • "str" (w/ or w/o interpolation)
    • :"sym" (w/ or w/o interpolation)
    • :sym
    • #{expr}
    • #@var
    • %w[...] and its family
    • <<EOF and its family
  • Constructor syntaxes
    • [elems]
    • { entries }
    • .. / ...
  • Variable-likes
    • my_var
    • MyConst
    • :: (prefix)
    • :: (infix)
    • @var
    • @@var
    • $foo
      • $foo
      • $" etc.
      • $-a etc.
      • $&
      • $1
    • self
    • __ENCODING__
    • __FILE__
    • __LINE__
    • it
    • _1
  • Calls
    • meth
    • obj.meth (also &. and ::)
    • obj.(args) (also &. and ::)
    • obj[idx]
    • `cmd` (w/ or w/o interpolation)
    • -> { body }
    • for lhs in arr
    • super (which is super(...))
    • super(args)
  • Operators
    • && / and
    • &&=
    • || / or
    • ||=
    • ! / not (has special semantics in old Rubies)
    • Ordinary operator expressions (which is method call)
      • ! / ~ / + (unary) / - (unary)
      • **
      • * / / / %
      • + / -
      • << / >>
      • &
      • | / ^
      • >, >=, <, <=
      • <=>, ==, !=, ===, =~, !~
      • not (alias of !)
    • Operator assignments (for binary non-predicate operators)
      • **=
      • *= / /= / %=
      • += / -=
      • <<= / >>=
      • &=
      • |= / ^=
  • Declarations
    • def meth (also endless variants)
    • def (recv).meth (also endless variants)
    • class
    • class <<expr
    • module
    • alias
    • undef
  • Controls
    • begin
    • if / unless (either prefix or infix)
    • else / elsif
    • while / until (either prefix or infix)
    • case / when / else (for classic switching)
    • case / in / else (for pattern-based switching)
    • ensure
    • rescue
    • rescue (infix operator)
    • return
    • break
    • next
    • yield
    • redo
    • retry
    • END
    • BEGIN
  • Arguments (also in Arrays and Hashes)
    • arg_expr as in arguments
    • *arr
    • key => value
    • key: value
    • **kwargs
    • &my_block
    • do |params| end / { |params| }
    • ... as in arguments
  • Predicates
    • defined?
    • expr in pat
    • expr => pat
  • Implicit syntaxes
    • Implicit array in assignment RHS
    • if a..b (i.e. flip-flop)
    • if /re/ (a.k.a. match last)
    • /(?<my_var>...)/ =~ val (note the capture)

LHS

  • Variable-likes
    • my_var
    • my_param = default_value
    • my_kwparam:
    • my_kwparam: default_value
    • MyConst (also note ::)
    • # shareable_const_value: literal
    • @var
    • @@var
    • $var (except $1 and $& variants)
  • Calls
    • obj.attr
    • obj[idx]
  • Splats
    • *args
    • **kwargs
    • **nil
    • ...
  • Multi
    • lhs1, lhs2
  • Decl
    • do |x, y; z|
  • Implicit syntaxes
    • implicit it capture (when the variable is used in the block body)
    • implicit _1 capture (when the variable is used in the block body)
    • implicit * (when the multi LHS ends with ,)

Patterns

  • |
  • [elems]
  • [*left, elems, *right]
  • my_var
  • { entries }
  • ^(expr) / ^my_var
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment