This question already has answers here:
Chaining methods using Symbol#to_proc shorthand in Ruby?
(4 answers)
Closed 5 years ago.
For example, I have enum.map(&:join).map(&:to_i). Is there a syntax in Ruby where I can write something like this: enum.map(&:join:to_i) in order to avoid iterating through the array twice using & operator?
You could use a block (with no &) to iterate just once:
enum.map { |e| e.join.to_i }
Related
This question already has answers here:
What are the restrictions for method names in Ruby?
(5 answers)
Closed 7 years ago.
On the Chef Style Guide page appears this Ruby expression:
antarctica_hint = hint?('antarctica')
What exactly does the ? after hint and before ('antarctica') mean? Is it just part of the method name? (i.e. the method is called 'hint?' not 'hint')
It is part of method name, and people typically (not always) use it for methods that return boolean value.
An example from Ruby is Class#respond_to?
This question already has answers here:
Can you supply arguments to the map(&:method) syntax in Ruby?
(9 answers)
Closed 8 years ago.
I wonder, isn't it possible to call a method using & operator with parameters?
items.each &:my_proc # ok
items.each &:my_proc(123, "456") # ops!
No, it's not possible. Use full form.
items.each{|i| i.my_proc(123, '456')}
Look at the source of Symbol#to_proc for the "why".
You can use a bit of trickery and acheive something similar:
class Symbol
def [](*args)
proc{|obj| obj.send(self, *args) }
end
end
[123.456, 234.567].map(&:round[2])
#=> [123.46, 234.57]
I highly discourage the use in production code though, since gems etc may rely on Symbol#[]. This is just a fun thing to play around with ;-)
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Understanding ruby splat in ranges and arrays
could anyone tell me what the * does in the following piece of code?
line = "name=yabbi;language=ruby;"
Hash[*line.split(/=|;/)]
Thanks.
* is the splat operator. It is used to split an array into a list of arguments.
line.split(/=|;/) returns an array. To create a Hash, each element of the array must be passed as an individual parameter.
it's a splat operator Read about it. Often times you see it used when you want to split up an array to use as parameters of a function.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Reverse a string in Ruby
in python using extended slice , an implementation for string reverse can be written like this
>>> 'hello world'[::-1]
'dlrow olleh'
i wonder how would one implement string reverse function in Ruby
>> "Hello world".reverse
=> "dlrow olleH"
This question already has an answer here:
naked asterisk as parameter in method definition: def f(*)
(1 answer)
Closed 10 years ago.
I was poking through the Rails 3 ActiveRecord source code today and found a method where the entire parameter list was a single asterisk.
def save(*)
I couldn't find a good description of what this does (though I have some ideas based on what I know about splat arguments).
What does it do, and why would you use it?
It means it can have any number of arguments (including zero) and it discards all those arguments.