how to do a one-liner c# function? in ruby

In C#:

To create a one-liner C# function, you can use lambda expressions or inline delegates. Here's an example:

main.cs
// Lambda Expression
Func<int, int, int> add = (x, y) => x + y;

// Inline Delegate
Func<int, int, int> multiply = delegate (int x, int y) { return x * y; };
158 chars
6 lines

In Ruby:

In Ruby, you can use a lambda or a block to create a one-liner function. Here's an example:

main.rb
# Lambda
add = lambda { |x, y| x + y }

# Block
multiply = ->(x, y) { x * y }
78 chars
6 lines

Note: In Ruby, the last line of a block is automatically returned, so you don't need to use the return keyword.

related categories

gistlibby LogSnag