Swapnil Gourshete Swapnil Gourshete Follow Sep 04, 2023 · 4 mins read
Keywords in Ruby
Share this

Keywords are special words in Ruby which are reserved by the language and have predefined uses. Each keyword serves a different implementation than other. Let’s dive into these keywords and understand their definitions.

  1. BEGIN -
    • Code inside BEGIN will run before any other code written in the current file.
  2. END -
    • Code inside END will run after any other code written in the current file
  3. alias -
    • alias helps to create an alias between two methods and It has lower precedence than && and is for short-circuit boolean
  4. begin -
    • This Ruby keyword helps us to start an exception handling block
  5. break -
    • It helps to leave a block early
  6. case -
    • This Ruby keyword helps to start a case expression
  7. class -
    • class helps user to create or opens a class in ruby
  8. def -
    • def keyword in ruby helps us to define a function
  9. defined? -
    • Defined? It describes its argument by returning a string
  10. do -
    • This Ruby keyword helps to start a block of code
  11. else -
    • Else is the unhandled condition in if and unless expression
  12. elsif -
    • Elsif helps the user to give alternate conditions to if expression
  13. end -
    • methods, modules, classes etc, use end. It indicates the end of a syntax block false False here means boolean false
  14. ensure -
    • ensure is a block of code that will always run when an exception is raised
  15. for -
    • To help us to create a loop for iteration
  16. if -
    • Used as an expression for if or modifiers if
  17. in -
    • It helps to separate iterable Objects and iterable variables in a for loop
  18. module -
    • Helps us in creating or opening module
  19. next -
    • Lets the user skips the rest of the code in the block
  20. nil -
    • Nil represents false value indicating “no value” or “unknown.”
  21. not -
    • It inverts the boolean expression but has less precedence than “!”
  22. or -
    • It represents boolean or with less precedence than “||”
  23. redo -
    • Redo helps to restart execution in the present block
  24. rescue -
    • It helps to start an exception section of code in a beginning block
  25. retry -
    • It helps us to retries an exception block
  26. return -
    • To exit from a method
  27. self -
    • Helps in calling Object of current method attached to
  28. super -
    • It helps in calling the current method in a superclass
  29. then -
    • In control structure, It represents the end of the conditional block
  30. true -
    • true here is just a simple boolean true expression
  31. undef -
    • It helps in preventing module or class from responding to the method call
  32. unless -
    • It helps in creating unless and modifiers unless expressions
  33. until -
    • Help us to create an iterative loop until a condition is met
  34. when -
    • it helps by representing a condition in a case expression
  35. while -
    • Help us to create an iterative loop while a condition is true
  36. yield -
    • yield Helps to start execution of block send to the current method
  37. ENCODING -
    • It represents script encoding of the current file
  38. LINE -
    • It represents the line number of keywords in the current file
  39. FILE -
    • It represents the path to the current file.

Happy Coding!!!

Swapnil Gourshete
Written by Swapnil Gourshete Follow
Hi I am Swapnil, a Software Engineer and computer science enthusiastic