Comparison of how to write Callback function (Java, JavaScript, Ruby)

Java

public class Sample {
  public static void func(Action<String> a) { 
    a.apply('Hello');
  }
  public static void main(String[] args) {
    Sample.func(s -> System.out.println(s));
    //For multiple lines
    Sample.func(s -> { 
      System.out.println(s)
      System.out.println(s)
    });
  }
}

JavaScript

const func = (callback) => {
  callback('Hello');
};

func((s) => console.log(s));
//For multiple lines
func((s) => {
  console.log(s)
  console.log(s)
});

Ruby

def func
  yield 'Hello'
end

func { |s| p s }
#In the case of multiple business lines
func do |s|
  console.log(s)
  console.log(s)
end
# {}But it works but do~end is better

Recommended Posts

Comparison of how to write Callback function (Java, JavaScript, Ruby)
[Ruby] How to write blocks
How to write Scala from the perspective of Java
[Java] Types of comments and how to write them
How to write Java variable declaration
java Eclipse How to debug javaScript
Basics of Java development ~ How to write programs (variables and types) ~
How to write offline real-time Java implementation example of F01 problem
[Java] How to use the hasNext function
[java] Summary of how to handle char
Summary of how to write annotation arguments
[Introduction to Java] How to write a Java program
[Java] How to output and write files!
[Java] [Maven3] Summary of how to use Maven3
[Processing × Java] How to use the function
Comparison of JavaScript objects and Ruby classes
JDBC promises and examples of how to write
How to find the cause of the Ruby error
[java] Summary of how to handle character strings
Java Development Basics ~ How to Write Programs * Exercise 1 ~
[Java] Summary of how to abbreviate lambda expressions
How to write code that thinks object-oriented Ruby
[Java] Memo on how to write the source
[Java] How to get the authority of the folder
How to write Java String # getBytes in Kotlin?
How to specify index of JavaScript for statement
Basics of Java development ~ How to write a program (flow and conditional branching) ~
Differences between Java, C # and JavaScript (how to determine the degree of obesity)
How to write Rails
How to write dockerfile
How to write docker-compose
How to write Mockito
From Java to Ruby !!
How to write migrationfile
[Java] How to get the URL of the transition source
[Java] How to use compareTo method of Date class
[Ruby on Rails] How to write enum in Japanese
java: How to write a generic type list [Note]
[Ruby] How to find the sum of each digit
[Java] How to get the maximum value of HashMap
As of April 2018 How to get Java 8 on Mac
How to write a date comparison search in Rails
Summary of Java communication API (1) How to use Socket
Summary of Java communication API (3) How to use SocketChannel
Summary of Java communication API (2) How to use HttpUrlConnection
Summary of how to implement default arguments in Java
How to write modern Java. Immutable Java, consider Null safety.
How to execute WebCamCapture sample of NyARToolkit for Java
Offline real-time how to write Implementation example of the problem in E05 (ruby, C11)
[Java] How to use Map
An unsupported Java version How to get rid of errors
How to lower java version
[Java] How to use Map
How to uninstall Java 8 (Mac)
How to write good code
How to use trained model of tensorflow2.0 with Kotlin / Java
Java to play with Function
Java --How to make JTable
How to use JSON data in WebSocket communication (Java, JavaScript)
How to add ActionText function
Difference between Java and JavaScript (how to find the average)