In groovy 2.4(which was released last week) there are three new methods added in “DefaultGroovyMethods” class they are “init“, “dropRight” and “takeRight“, lets take them one by one
takeRight e.g
Returns the last num elements from the tail of this List.
def strings = [ 'a', 'b', 'c' ] assert strings.takeRight( 0 ) == [] assert strings.takeRight( 2 ) == [ 'b', 'c' ] assert strings.takeRight( 5 ) == [ 'a', 'b', 'c' ]
and dropRight
Drops the given number of elements from the tail of this List.
def strings = [ 'a', 'b', 'c' ] assert strings.dropRight( 0 ) == [ 'a', 'b', 'c' ] assert strings.dropRight( 2 ) == [ 'a' ] assert strings.dropRight( 5 ) == []
and init
Returns the items from the Iterable excluding the last item. Leaves the original Iterable unchanged.
def list = [3, 4, 2] assert list.init() == [3, 4] assert list == [3, 4, 2]
Above examples are taken straight from API 😉 .
Recent Comments