The second time is a class or method.
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
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.
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.
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.
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 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
Methods are divided into those with no arguments and those with 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
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
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
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
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