Why would one use the unary operator on a property in ruby? i.e &:first
Read the answers in the duplicate questions for the meaning and usage of &:...
. In this case, entries
is an array, and there are three methods map
, sort_by
, and map
chained. sort_by(&:last)
is equivalent to sort_by{|x| x.last}
. map(&:first)
is the same as map{|x| x.first}
. The reason the first map
does not use &:...
is because (i) the receiver of accept_entry
is not e
, and (ii) it takes an argument e
.
what is the functionality of &: operator in ruby?
There isn't a &:
operator in Ruby. What you are seeing is the &
operator applied to a :symbol
.
In a method argument list, the &
operator takes its operand, converts it to a Proc
object if it isn't already (by calling to_proc
on it) and passes it to the method as if a block had been used.
my_proc = Proc.new { puts "foo" }
my_method_call(&my_proc) # is identical to:
my_method_call { puts "foo" }
So the question now becomes "What does Symbol#to_proc
do?", and that's easy to see in the Rails documentation:
Turns the symbol into a simple proc, which is especially useful for enumerations. Examples:
# The same as people.collect { |p| p.name }
people.collect(&:name)
# The same as people.select { |p| p.manager? }.collect { |p| p.salary }
people.select(&:manager?).collect(&:salary)
what does &: mean in ruby, is it a block mixed with a symbol?
When &
used before Proc object in method invocation, it treats the Proc as if it was an ordinary block following the invocation.
When &
used before other type of object (symbol :first_name
in your case) in method invocation, it tries to call to_proc on this object and if it does not have to_proc method you will get TypeError
.
Generally &:first_name
is the same as &:first_name.to_proc
.
Symbol#to_proc Returns a Proc object which respond to the given method by sym.
:first_name.to_proc
will return Proc that looks like this:
proc { |obj, *args, &block| obj.first_name(*args, &block) }
this Proc invokes method specified by original symbol on the object passes as the first parameter and pass all the rest parameters + block as this method arguments.
One more example:
> p = :each.to_proc
=> #<Proc:0x00000001bc28b0>
> p.call([1,2,3]) { |item| puts item+1 }
2
3
4
=> [1, 2, 3]
What does temps.each(&:valid?) mean in Ruby?
The &
is called the to_proc
operator. It expands the symbol (:valid?
) into a Proc.
So your example is equivalent to:
temps.each { |t| t.valid? }
Ruby ampersand colon shortcut
Your question is wrong, so to speak. What's happening here isn't "ampersand and colon", it's "ampersand and object". The colon in this case is for the symbol. So, there's &
and there's :foo
.
The &
calls to_proc
on the object, and passes it as a block to the method. In Ruby, to_proc
is implemented on Symbol
, so that these two calls are equivalent:
something {|i| i.foo }
something(&:foo)
So, to sum up: &
calls to_proc
on the object and passes it as a block to the method, and Ruby implements to_proc
on Symbol
.
What does map(&:name) mean in Ruby?
It's shorthand for tags.map(&:name.to_proc).join(' ')
If foo
is an object with a to_proc
method, then you can pass it to a method as &foo
, which will call foo.to_proc
and use that as the method's block.
The Symbol#to_proc
method was originally added by ActiveSupport but has been integrated into Ruby 1.8.7. This is its implementation:
class Symbol
def to_proc
Proc.new do |obj, *args|
obj.send self, *args
end
end
end
what is the difference between += and =+ in ruby?
There's no such token as =+
; it's actually two tokens: assignment followed by the unary +
operator; the latter is essentially a no-op, so @@num_things =+ 1
is equivalent to @@num_things = 1
.
Since there is a +=
token, the language parser will parse it as a single token.
(In the early formulations of BCPL which was the precursor to C, the modern -=
operator was written as =-
.)
What is Ruby's double-colon `::`?
::
is basically a namespace resolution operator. It allows you to access items in modules, or class-level items in classes. For example, say you had this setup:
module SomeModule
module InnerModule
class MyClass
CONSTANT = 4
end
end
end
You could access CONSTANT
from outside the module as SomeModule::InnerModule::MyClass::CONSTANT
.
It doesn't affect instance methods defined on a class, since you access those with a different syntax (the dot .
).
Relevant note: If you want to go back to the top-level namespace, do this: ::SomeModule – Benjamin Oakes
What does this ampersand mean?
It's basically shorthand for this:
[Category, Product, Person].each { |e| e.delete_all }
That is, it sends delete_all
to each element of the iterator.
Related Topics
Serving Static Files With Sinatra
Pass a Variable into a Partial, Rails 3
Learning Insertion Sort in Ruby
How to Access Method Arguments in Ruby
Installing Nokogiri on Osx 10.10 Yosemite
How to Create a Class Instance from a String Name in Ruby
In Ruby, How Does Coerce() Actually Work
Using Rails Migration on Different Database Than Standard "Production" or "Development"
Why Do I Get a "Permission Denied" Error While Installing a Gem
How to Search a Folder and All of Its Subfolders For Files of a Certain Type
Automatic Counter in Ruby For Each
Getting Fields_For and Accepts_Nested_Attributes_For to Work With a Belongs_To Relationship
"Gem Install Rails" Fails With Dns Error
Shortcut to Make Case/Switch Return a Value