0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Callback関数の書き方の比較(Java, JavaScript, Ruby)

Last updated at Posted at 2019-07-23

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));
    // 複数行の場合
    Sample.func(s -> { 
      System.out.println(s)
      System.out.println(s)
    });
  }
}

JavaScript

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

func((s) => console.log(s));
// 複数行の場合
func((s) => {
  console.log(s)
  console.log(s)
});

Ruby

def func
  yield 'Hello'
end

func { |s| p s }
# 複数業行の場合
func do |s|
  console.log(s)
  console.log(s)
end
# {}でも動作するがdo~endがベター
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?