Ruby syntax summary

Ternary operator (conditional operator)

?Processing when true:Processing when false

Case syntax

case Target object or expression
when value 1
 #Processing for value 1 then
when value 2 then#What to do if the value 2 is true(If you use then, you only need one line)  
 #What to do if none of them match

-Since the last evaluated expression is the return value, it is also possible to put the result in a variable. -The value after when can be a hash such as: japan.


class class name(The first letter is uppercase. Camel case)

-Since Ruby uses single inheritance, one class can only have one superclass. -If you include a module in a class, the method defined in the module can be called as an instance method. -Adding a function by including or extending a module to a class is called a mixin, and a mechanism similar to multiple inheritance can be realized with this. Since include can be called as an instance method and extend as a singular method (class method), extend when you want to call it directly under the class syntax. -A class is an instance (object) of the class class. It inherits the Module class that inherits the Object class.

-How to check the included modules
> User.include?(Module name) 
 => #returns true or false
> User.included_module 
 => [Kernel] #Included modules are returned as an array
> User.ancestors
 => [Bank, Object, Kernel, BasicObject] #Modules and superclasses returned


module module name
Module definition. Methods, constants, etc.

-Unlike classes, instances cannot be created from modules. (I get an error with user = -Other modules and classes cannot be inherited. It cannot be inherited because the is-a relationship does not hold, but it is used when you want to have common functions .. -When mixing in a class, if it is not necessary to make it public, it is better to make it private on the module side. -Since the module self becomes the instance of the include destination, if the included class has a price method, the price can be called on the module side as well. -The reason why puts and loop methods can be used at any time in Ruby is that the Object class at the top of virtually all classes includes the Karnel module. (Contents of Karnel module) -The module is an instance of the module class. It inherits the Object class.

Exception handling

The most basic syntax.

 #Processing that can cause exceptions
 #What to do if an exception occurs


 #Processing that can cause exceptions
 (#In many cases, else is unnecessary because you can write the processing when an exception does not occur here.)
 (#What to do if an exception occurs.)
 #What to do if no exception occurs
 #Process to be executed regardless of the presence or absence of an exception

・ Rescue is not mandatory. You can end abnormally, but if there is a process you want to do before it ends, you can just ensure -However, in cases such as passing a block to the open method and "always releasing the source when used", it can often be processed automatically by using a method with a block, so the ensure method may not come into play.

def method name
 case object
 when :japan
 when :us 
  raise "Error message" 
  #=> RuntimeError: "Error message"
  #If nothing is specified, RuntimeError will be called.

・ If you want to call something other than RuntimeError, refer to the following. ① raise ArgumentError," error message "raise ("error message")


A lump of processing. You can objectify blocks with Proc In Ruby, you can use blocks when calling methods.

Method summary

1 2
chomp Remove newline character
brock.given? Returns true if a block is passed

Recommended Posts

Ruby syntax summary
ruby basic syntax memo
[Ruby] Method definition summary
ruby qualification test point summary
Ruby syntax errors and countermeasures
Ruby on Rails validation summary
[Java] Summary of control syntax
Ruby on Rails Overview (Beginner Summary)
Ruby on Rails variable, constant summary
Ruby environment construction summary ~ mac version ~
Ruby learning 4
[Ruby] Array
Ruby basics
Ruby learning 5
Ruby basics
Object-oriented summary
Ruby Review 2
Ruby addition
Refactoring Ruby
Ruby learning 3
part of the syntax of ruby ​​on rails
Summary of hashes and symbols in Ruby
Ruby: Regular expression summary * Code sample available
Ruby problem ⑦
Ruby learning 2
[Ruby] Block
ruby calculator
Ruby learning 6
Ruby settings 1
Refactoring Ruby
Ruby basics
Ruby memo
Ruby learning 1
Ruby Review 1
[Ruby] Module
[Ruby] Summary of class definitions. Master the basics.