What Is the -≫ (Stab) Operator in Ruby

What is the - (stab) operator in Ruby?

It's the Ruby 1.9 "stabby lambda" operator. For example, see this article from 2008.

Nutshell:

> foo2 = ->(arg) { arg * 2 }
> foo2.call "now"
=> nownow

Note the lack of space between -> and (arg), that's intentional.

What do you call the - operator in Ruby?

In Ruby Programming Language ("Methods, Procs, Lambdas, and Closures"), a lambda defined using -> is called lambda literal.

succ = ->(x){ x+1 }
succ.call(2)

The code is equivalent to the following one.

succ = lambda { |x| x + 1 }
succ.call(2)

Informally, I have heard it being called stabby lambda or stabby literal.

What does - operator in Ruby mean?

That's a new syntax for lambda. You can also write it like this:

subject.post_source = lambda { new_post }

Here's how old and new versions look like with parameters (thanks to Michael Kohl for suggestion):

v_old = lambda {|a, b| a + b}
v_new = ->(a, b) { a + b}

v_old.call(1, 2) # => 3
v_new.call(3, 4) # => 7

What does - mean in Ruby

It is a lambda literal. Check this example:

 > plus_one = ->(x){x+1}
=> #<Proc:0x9fbaa00@(irb):3 (lambda)>
> plus_one.call(3)
=> 4

A lambda literal is a constructor for Proc. A Proc is a way to have a block of code assigned to a variable. After this, you can call your block of code again, with different arguments, as many times as you wish.

This is how you can pass a "function" as parameter in ruby. In many languages, you could pass a reference to a function. In ruby, you can pass a Proc object.

What is the - (dash greater than) operator in Ruby/Rails

This is syntactic sugar.

->(external_id) { where(external_id: external_id) }

is equal to:

lambda { |external_id| where(external_id: external_id) }

What is -() { } in Ruby?

It is a lambda literal. Put the block variables inside () and the body inside {}.

->(x, y){x + y}

In the example, ->(v){v} takes a single argument v and returns it, in other words, it is an identity function. If a block is passed to method, then that is assigned to c. If not, the identity function is assigned to c as default.

what does ruby symbol - do?

what does ->(input) do?

->() { .. } is called staby proc.

And when to use this symbol -> in ruby?

When you want to use a Proc object using Kernel#lambda method. ->() { .. } is a syntactic sugar of Kernel#lambda.

Ruby 1.9.1 introduces this new, more concise syntax for creating lambda methods.

The stab operator is named for its resemblance to a knife or stabbing motion: ->. Following the stab portion of the operator, there is a argument list, just as with in a normal method. Then, a normal Ruby block in braces.

Since the lambda's argument list is a formal argument list, as opposed to a block argument list, several other features such as default argument values are supported. A lambda is somewhere in between an anonymous block or closure and a formal named method.

Why would you use a !! operator

You use it if you only want the boolean, not the object. Any non-nil object except for boolean false represents true, however, you'd return the data as well. By double negating it, you return a proper boolean.

How do I create a reusable block/proc/lambda in Ruby?

Create a lambda and then convert to a block with the & operator:

isodd = lambda { |i| i % 2 == 1 }
[1,2,3,4].select(&isodd)

Kernel.lambda() not returning a lambda Proc in Ruby 1.9

I would suggest reading this: http://ruby-doc.org/core-1.9.3/Proc.html#method-i-lambda-3F



Related Topics



Leave a reply



Submit