The ampersand operator with parameters [duplicate] - ruby

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 ;-)

Related

no implicit conversion of Symbol into Integer, Ruby [duplicate]

This question already has an answer here:
Ruby, no implicit conversion of Symbol into Integer
(1 answer)
Closed 6 years ago.
I am new to ruby, and have no idea, how to fix this error. when i run my script containing the code below, i always get the error: 'no implicit conversion of Symbol into Integer. I think the Problem is in the lines #killProc..., #Name=..., and #working_directory.
Can you tell me whats wrong?
Thanks for your help
class RubyCommand
include Patir::Command
attr_reader :cmd,:working_directory,:killProc
def initialize params,&block
#killProc=params[:killProc]
#name=params[:name]
#working_directory=params[working_directory]||"."
if block_given?
#cmd=block
else
raise "You Need to provide a block"
end
end
end
Pretty sure you're passing an array instead of a hash. You should call it like this:
RubyCommand.new({killProc:1,name:"test"})

ruby equivalent of destructor [duplicate]

This question already has an answer here:
Notification of object destruction in Ruby
(1 answer)
Closed 9 years ago.
I'm using RubyPython to import a Python module. I am doing RubyPython.start in the constructor (initialize), and I suppose I should symmetrically do RubyPython.stop in the destructor, but unfortunately it seems that there is no destructor in Ruby:
class QDSHiveHelper
def initialize
RubyPython.start
qds = RubyPython.import('blah')
...
end
def do_something
qds.some_function
...
end
def finalize
RubyPython.stop
end
end
Could someone please explain how to accomplish this? ObjectSpace.define_finalize seems to be discouraged and has some gotchas (can't use closure etc). I could also just leave RubyPython dangling and not call stop on it, but I don't know what could be the consequences. What's the best way out?
There is a hook called ObjectSpace.define_finalizer that is called when an object is destroyed.

whats does .map(&:chomp) do, exactly? [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
What do you call the &: operator in Ruby?
I see '.map(&:chomp)' all the time
I know what chomp and map do, but I want to know what &: does and I'd like to know why I can't find it on the web after 30 minutes of googling.....
It's Symbol#to_proc, and it turns the symbol into a proc which attempts to invoke the given method on its argument, returning the result.
x = :reverse.to_proc
x.call("asdf") # "fdsa", like calling "asdf".reverse
In your case, .map(&:chomp) is equivalent to .map { |x| x.chomp }.
If you can't find it by Googling, it's because you're Googling the wrong thing. It's a well-known Ruby idiom.

What does a single splat/asterisk in a Ruby argument list mean? [duplicate]

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.

`&:views_count` in `Post.published.collect(&:views_count)` [duplicate]

This question already has answers here:
What does map(&:name) mean in Ruby?
(17 answers)
Closed 8 years ago.
I saw the code from here
Post.published.collect(&:views_count)
I guess it equals to
.collect { |p| p.views_count }
But I never saw this usage before, does this have a name? Where can I find more information about it?
This is actually a rather clever hack made it into ruby 1.9.
Basically, & in front of a variable in ruby coerces it into a proc. It does that by calling to_proc. Some clever fellow (first time I saw this was in _whys code, but I won't credit him cause I don't know if he came up with it) added a to_proc method to Symbol, that is essentially {|obj| obj.send self}.
There aren't many coercians in ruby, but it seems like all of them are mostly used to do hacks like this (like !! to coerce any type into a boolean)
It's a use of Symbol#to_proc. The & operator turns a Proc object into a block, and because Ruby 1.8.7 and newer implement Symbol#to_proc, it can be used with a symbol like :views_count. And yes, it's equivalent to {|p| p.views_count}.

Resources