Is it an operator? If it is, how is it being used? Or does it point to something? I've looked for examples of it's usage in the inject method but all I can really gather is that it somehow acts like { |sum, n| sum + n }.
Asked
Active
Viewed 66 times
2 Answers
4
:+
is a symbol.
You can see it in IRB:
> :+.class
=> Symbol
In this context, it actually represents the +
method that will be invoked instead of a block by inject
.
You can call it dynamically yourself, like this:
> 5.send(:+, 3)
=> 8
which is equivalent to:
> 5 + 3
=> 8

SirDarius
- 41,440
- 8
- 86
- 100
-
Thank you very much, that clears it up a bit. So any thing that starts with ":" is a symbol. So I know that in ruby you can't pass operators as arguments to functions/methods. Though it seems that this is an acting work-around, is that correct? – specifically_user3827668 Jan 25 '15 at 03:19
-
@user3827668, You can't pass ANY methods as arguments. Why? Because in ruby when you write a method name like dostuff, ruby executes the method--parentheses are optional. So, whenever you write a method name, ruby executes the method, making it impossible to pass the method itself. Therefore, you have to pass a string that is the method name: "do_stuff" or a symbol, which for now you can assume is identical to a string, :dostuff, and then use the string to execute the method. – 7stud Jan 25 '15 at 03:38
-
So "+" is not an operator, but rather a method? – specifically_user3827668 Jan 25 '15 at 03:54
0
You can check this How does "(1..4).inject(&:+)" work in Ruby
and this What are :+ and &:+ in Ruby?
then you will totally understand :)

Community
- 1
- 1

xiongbo027
- 96
- 5