Skip to content

Instantly share code, notes, and snippets.

@diego-aslz
Created December 12, 2013 12:00
Show Gist options
  • Select an option

  • Save diego-aslz/7926987 to your computer and use it in GitHub Desktop.

Select an option

Save diego-aslz/7926987 to your computer and use it in GitHub Desktop.
Proc in Java
package main;
public class DomainClass {
public void doSomething(Object arg0, Proc block) {
// do something
block.run(arg0);
}
}
package main;
public class Main {
public static void main(String[] args) {
DomainClass mc = new DomainClass();
mc.doSomething("some argument", new Proc() {
public Object run(Object ... args) {
System.out.println("Proc with: " + args[0]);
return null;
}
});
}
}
package main;
public interface Proc {
public Object run(Object ... args);
}
@diego-aslz
Copy link
Author

Is this the best way to simulate Procs in Java?

@caarlos0
Copy link

I believe so, this is probably the closest you can get from Ruby Procs in Java, sadly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment