What's the equivalent of ruby blocks in java

What's the equivalent of ruby blocks in java

what'd
what meaning
what definition
what w
what does
what is called
how to pronounce what
what h

Ruby blocks are handy for wrapping some code around some block defined by the caller.

How to do this in Java?

def myf(i)
  puts "start"
  yield i
  puts "end"
end

myf(3){ |i| puts i * 10 }
myf(4){ |i| puts i - 10 }

# outputs:
# start
# 30
# end
# start
# -6
# end

How to do this in Java?

There is no equivalent to Ruby blocks in Java. Ruby blocks are syntactically lightweight, semantically lightweight, and they are not objects. They are mostly syntactic constructions with some lightweight semantics behind. In this, they are more like an enhanced for loop in Java.

The closest equivalent you have in Java, would be a functional interface combined with a lambda expression. Something like this, using one of the pre-defined functional interfaces from the java.util.function package, namely the interface java.util.function.Consumer<T>:

void myf(int i, java.util.function.Consumer<Integer> proc) {
    System.out.println("start");
    proc.accept(i);
    System.out.println("end");
}

You use it like this:

myf(3, i -> System.out.println(i * 10));
myf(4, i -> System.out.println(i - 10));
// start
// 30
// end
// start
// -6
// end

However, this is not equivalent to Ruby's blocks. This is equivalent to Ruby's Procs and lambda literals, in other words, it is more equivalent to this:

def myf(i, proc)
  puts "start"
  proc.(i)
  puts "end"
end

myf(3, -> i { puts i * 10 })
myf(4, -> i { puts i - 10 })

# start
# 30
# end
# start
# -6
# end

Note that myf in your example does not use the result of the block, so modeling it with java.util.function.Function<T, R> would be incorrect. Consumer<T> is the correct interface to use for a "function" (more a procedure, really) that "consumes" its argument but doesn't return anything, whereas Function<T, R> is the correct interface to use for a function that takes one argument and returns a result.

What, P!nk's official music video for 'So What'. Click to listen to P!nk on Spotify: http:// smarturl.it Duration: 3:46 Posted: Oct 25, 2009 WhatsApp works with Safari 9+ To use WhatsApp, update Safari or use Google Chrome, Mozilla Firefox or Opera.. Update Safari


Your own answer is correct that lambdas (combined with functional interfaces) make this possible in Java, but it can be much less complicated than that by using the types in java.util.function. Here's that code using Function<T,R>:

myf:

public static int myf(int i, Function<Integer,Integer> fn) {
    System.out.println("start");
    int retval = fn.apply(i);
    System.out.println(retval);
    System.out.println("end");
    return retval;
}

Using it:

public static void main(String[] args) {
    myf(3, i -> i * 10);
//         ^^^^^^^^^^^−−−−−−−−−−−− lambda expression
    myf(4, i -> i - 10);
//         ^^^^^^^^^^^−−−−−−−−−−−− lambda expression
}

Live example

I used Function because your Java code seemed to show that you wanted a return value. If you don't want a return value, just use Consumer<T>.

P!nk - So What (Official Music Video), I will do what I can to help you. TranslationsEdit. show ▽what? (interrogative pronoun)� noun the true nature or identity of something, or the sum of its characteristics: a lecture on the whats and hows of crop rotation.


In Java 1.8, Lambda expression can be used to achieve the desired result:

public class Temp {
    interface BlockBody {
        int op(int b);
    }

    public static int myf(int i, Temp.BlockBody blockBody) {
        System.out.println("start");
        int out = blockBody.op(i);
        System.out.println(out);
        System.out.println("end");
        return out;
    }


    public static void main(String[] args) {
        // block lambda function defined here
        Temp.BlockBody blockBody = (int a) -> {
            int c = a * 10;
            return c;
        };
        int result = Temp.myf(3, blockBody);

        // block body is changed
        Temp.BlockBody blockBody2 = (int a) -> {
            int c = a - 10;
            return c;
        };
        int result2 = Temp.myf(4, blockBody2);
    }
}

// outputs:
// start
// 30
// end
// start
// -6
// end

what, what definition: 1. used to ask for information about people or things: 2. used in questions that show you are…. Learn more. what definition: 1. used to ask for information about people or things: 2. used in questions that show you are…. Learn more.


WHAT, Quickly send and receive WhatsApp messages right from your computer. whatever: adverb at all , of any description , of any kind or sort , whatsoever , whichever


WhatsApp Web, Here's What We Know. President Trump is talking about banning the app. TikTok may also sell its U.S. operations. Let's sort through it all here. Desperate to secure funding for her med tech startup, an idealistic scientist and her husband strike an outrageous deal with a mysterious investor. Watch trailers & learn more.


What's Going On With TikTok? Here's What We Know, It is plural if a plural noun or noun phrase completes the sentence, as in He sometimes makes what seem to be gestures of reconciliation. � Clauses with what as� Who definition is - what or which person or persons —used as an interrogative —used by speakers on all educational levels and by many reputable writers, though disapproved by some grammarians, as the object of a verb or a following preposition.