?Processing when true:Processing when false
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) else #What to do if none of them match end
-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) end
-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.
> 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. end
-Unlike classes, instances cannot be created from modules. (I get an error with user = User.new) -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.
The most basic syntax.
begin #Processing that can cause exceptions rescue #What to do if an exception occurs end
begin #Processing that can cause exceptions (#In many cases, else is unnecessary because you can write the processing when an exception does not occur here.) (rescue) (#What to do if an exception occurs.) (else) #What to do if no exception occurs (ensure) #Process to be executed regardless of the presence or absence of an exception end
・ 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 'yen' when :us 'dollar' else raise "Error message" #=> RuntimeError: "Error message" #If nothing is specified, RuntimeError will be called. end end
・ If you want to call something other than RuntimeError, refer to the following.
raise ArgumentError," error message "
raise ArgumentError.new ("error message")
A lump of processing. You can objectify blocks with Proc In Ruby, you can use blocks when calling methods.
|chomp||Remove newline character|
|brock.given?||Returns true if a block is passed|