Java engineers now compare to learn the basic grammar of Ruby Part 2 (classes, methods)

Introduction

The second time is a class or method.

environment

paiza.io It is easier to execute in such a case

The version when this was made is as follows. You can check the version from Help.

Ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux] Java openjdk version "12" 2019-03-19

Scope story

Both Java and Ruby have public, protected, and private, but since the concept is different, the terms are separated.

public is the same and has no restrictions, but protected and private are different.

For details, I wrote it in a Java engineer-like interpretation while referring to here.

The concept of package private doesn't seem to exist in Ruby.

Class scope

There are public (not described) and private. I don't see the situation where I use a class that is not an inner class as a private class because I don't seem to have a chance to use it on the Web (impression so far). You will additionally learn what kind of scope it will be if necessary.

I feel like I haven't used inner classes in Java recently, so I won't cover the scope in this article. Please note.

Method scope

There are public (not described), protected, and private.

It seems good to think that public is the same, so I will describe two different things.

private I will put it from the person who was easy to verify the difference. First of all, please see the sample code below.

class A
    private def print
       p "a"
    end
end

class B < A
    def print2
        print
    end
end

b = B.new
b.print2

Unlike Java, Ruby's private works even if you use it from other than the class. Looking at the article I referred to, it says that the method set to private can only be called in the function format, so it can also be called from the subclass that can be called in the function format. It's working fine, but it's a bit unpleasant for a Java engineer, so be careful.

Maybe I could have done it if I thought I couldn't access it in other cases.

protected I did a lot of research, but I didn't see it because I often didn't understand it and I saw a tweet saying that the creator, Matz, wouldn't incorporate it now.

I think it's okay for beginners not to see it until they witness a scene that they absolutely have to remember.

class

There is no particular problem here. The user side generates and uses it in the form of class name.new.

public class A {
}
class A
end

However, Ruby can extend the class in the following way, so if you want to do it, you can do anything.

class A
    def print
       p "a"
    end
end

class A
    def print2
        p "b"
    end
end

a = A.new
a.print
a.print2

You can also extend to the created instance, so you can create a world that is truly anything. This can be limited to the created instance, so it may not be said that it is useless.

class A
    def print
       p "a"
    end
end

a = A.new

#Declare as a singular class
class << a
    def print2
        p "b"
    end
end

a.print
a.print2

Inheritance

Inheritance is single inheritance in both Java and Ruby.

However, Java can be used for multiple interfaces, so it is possible to do something a little forcibly (especially Java 8 or later).

Ruby doesn't seem to support abstract classes, methods, and interfaces linguistically, so full inheritance is a single class inheritance. There are mixins and delegations (you need to use a library), but that seems to be a completely different concept, and it seems better to think of it as separate from inheritance. (Not covered in this article, module is also excluded once)

public class A extends B {
}
class A < B
end

Method

Methods are divided into those with no arguments and those with arguments.

No arguments

Ruby allows you to omit (). Note that Ruby does not need to declare a return value, so it is difficult to understand unless you write Docs. The return value of Ruby describes return, or the result of the last evaluated expression is returned. Java has to clearly state the return.

public void method() {
    System.out.println("a");
}
def method
    print "a"
end

Ruby can be omitted even when calling. I'm surprised, so I think it's better to set as a rule what kind of description should be used without it.

method()
method

With arguments

Ruby can omit () even if there is an argument. The same is true for the caller.

public void method(String a, String b) {
    System.out.print(a);
    System.out.print(b);
}
def method a, b
    print a
    print b
end

Method nesting

You can't do it in Java, but in Ruby you can declare a method inside a method. It can be used after nesting and declaring it, and cannot be used before.

def method
    print "a"
    
    def methtod2
        print "b"
    end

    methtod2
end

Class method

Ruby can belong methods to classes. In Java, it feels like you can use it in the same way as a static method (I think the idea is different).

public class A {
    public static void main() {
        System.out.print("a");
    }
}
class A
    def self.method
        print "a"
    end
end

A::method

If you declare it as a singular method, it is possible to make all of them class methods. Since the reference says that it is suitable for defining multiple methods, it seems better to divide it in an easy-to-understand manner.

class A
    def print
       p "a"
    end
end

class << A
    def print2
        p "b"
    end
end

a = A.new
a.print
A::print2

It is also possible to generate a class method without declaring it from the class if you want to do it alone.

def A.print2
    p "b"
end

in conclusion

I wrote it out for the time being, but I felt that it was easier to create the cause of chaos, so I should firmly create and operate team rules. And there are things I haven't written yet, but for the time being, I felt that it was just enough for beginners to start, so I will stop here.

The freedom of Ruby is confusing because Java is so solid.

Recommended Posts

Java engineers now compare to learn the basic grammar of Ruby Part 2 (classes, methods)
Java engineers now compare to learn the basic grammar of Ruby Part 1 (Basic, Variables)
I tried to summarize the basic grammar of Ruby briefly
[Java] Personal summary of classes and methods (basic)
Basic methods of Ruby hashes
Basic methods of Ruby arrays
Ruby methods and classes (basic)
Review of Ruby basic grammar
Be sure to compare the result of Java compareTo with 0
[Java] Various summaries attached to the heads of classes and members
Java to learn with ramen [Part 1]
Basic usage of java Optional Part 1
Connecting to a database with Java (Part 1) Maybe the basic method
I tried to summarize the methods of Java String and StringBuilder
I translated the grammar of R and Java [Updated from time to time]
Introduction to Ruby basic grammar with yakiniku
part of the syntax of ruby ​​on rails
Compare the elements of an array (Java)
Memorandum (Ruby: Basic Grammar: Classes and Instances)
[Java] I personally summarized the basic grammar.
Output of the book "Introduction to Java"
[Ruby on Rails] How to make the link destination part of the specified id
[Promotion of Ruby comprehension (1)] When switching from Java to Ruby, first understand the difference.
I tried to compare the infrastructure technology of engineers these days with cooking.
How to find the cause of the Ruby error
About the idea of anonymous classes in Java
The story of introducing Ajax communication to ruby
[Java] The confusing part of String and StringBuilder
[Ruby] Code to display the day of the week
[Java] How to get the authority of the folder
Java Welcome to the Swamp of 2D Arrays
The part I was addicted to in "Introduction to Ajax in Java Web Applications" of NetBeans
Java basic grammar
Java basic grammar
Java basic grammar
Java basic grammar
[Java] How to get the URL of the transition source
[Java] Basic summary of Java not covered by Progate ~ Part 1 ~
How to write Scala from the perspective of Java
[Ruby] How to find the sum of each digit
[Java] How to get the maximum value of HashMap
Memorandum (Ruby: Basic grammar: Use by naming the process)
Java classes and instances to understand in the figure
Introduction to Java for beginners Basic knowledge of Java language ①
Java: Use Stream to sort the contents of the collection
~ I tried to learn functional programming in Java now ~
What Java engineers need to prepare for the Java 11 release
Replace only part of the URL host with java