This question already has answers here:
Ruby: Is there an opposite of include? for Ruby Arrays?
(13 answers)
Closed 4 years ago.
I have a question: Is there any opposite method of .include? I know with unless, but I want to do it with if, can I? I tried with unless:
unless variable.include?("something")
#..
end
I want to do it with if, can I? I know .!include? but it didn't work(i don't really know if this method exists, but I saw it in this forum).
if !variable.include?("something")
in plain Ruby
if variable.exclude?("something")
in Rails
Related
This question already has answers here:
What does map(&:name) mean in Ruby?
(17 answers)
Closed 3 years ago.
I'm walking through the example project in the Rails 5 Test Prescriptions - Build a Healthy Codebase (published date: 2018) book and encountering this method:
#pages 29-30 of the book
class Project
.
.
def done?
tasks.all?(&:complete?) #only this line confused me, especially the `&` part
end
end
the syntax looks really strange to me since I just learned Ruby & Rails for more than one month..any hints just for pointing me to where I should read would be really appreciated
& is for passing block to method as a block (also used the other way in parameter list to make implicit block a parameter), it implicitly calls to_proc on passed object.
Symbol#to_proc for :symbol makes a proc{|param| param.symbol }
So your code is equvalent to tasks.all?{|task| task.complete? }
This question already has answers here:
What does map(&:name) mean in Ruby?
(17 answers)
Closed 9 years ago.
Can any one explain the use of '&' in this piece of ruby code?
s.feature_addons.select(&:is_active?)
It means:
s.feature_addons.select { |addon| addon.is_active? }
The & calls to_proc on the object, and passes it as a block to the method.
class Symbol
def to_proc
Proc.new { |*args| args.shift.__send__(self, *args) }
end
end
U can define to_proc method in other classes: Examples
That's a shortcut for to_proc. For example, the code you provided is the equivalent of:
s.feature_addons.select {|addon| addon.is_active?}
Some old documentation for it can be found here:
http://apidock.com/rails/Symbol/to_proc (when it was provided by ActiveSupport)
It then became a part of Ruby core in 1.9
http://www.ruby-doc.org/core-1.9.3/Symbol.html
You can use this syntax as shorthand for methods to apply to an entire collection.
It is functionally the same as:
s.feature_addons.select { |a| a.is_active? }
You can use it with any collections, such as:
User.all.map(&:id)
etc
This question already has answers here:
Is there an inverse 'member?' method in ruby?
(5 answers)
Closed 8 years ago.
So a fairly common pattern I've run up against is something like this:
[:offer, :message].include? message.message_type
The inversion of wording there messes me up. So I wrote this little monkey patch for Symbol in specific.
def in? *scope
scope.include? self
end
So now I can do the previous this way:
message.message_type.in? :offer, :message
This works fine and I'm happy with it, but occasionally I need similar functionality for other objects. Model objects in Rails apps being the most common case but strings occasionally, etc.
What kind of issues would I run into if I monkey patched this directly into Object?
Rails (ActiveSupport) already patches Object with this method. Here is the documentation: http://api.rubyonrails.org/classes/Object.html#method-i-in-3F.
Returns true if this object is included in the argument. Argument must be any object which responds to #include?. Usage:
characters = ["Konata", "Kagami", "Tsukasa"]
"Konata".in?(characters) # => true
This will throw an ArgumentError if the argument doesn’t respond to #include?.
This question already has an answer here:
What does map(&:name) do in this Ruby code?
(1 answer)
Closed 8 years ago.
For example, if I use rename method in mongo ruby driver, I can check the code here
What exactly is happening when I am using map(&:attributes)?
I think this means tags.map(&:attributes.to_proc).join(' '), but I am not sure why I am getting "undefined method `each_pair' for Arrayxxxxx" error with this command:
TableA.create(TableB.all.map(&:attributes))
Any insight will be appreciated
map returns an array of whatever is returned by the method call.
so
TableB.all.map(&:attributes)
is basically an array of
[TableB.all[0].attributes,TableB.all[1].attributes,TableB.all[2].attributes,...]
Do you want something like
TableB.all.map(&:attributes).each do |attr|
TableA.create(attr)
end
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Make blank params[] nil
Is there a way to make blank form inputs submit nil? Right now, I'm going through and in a before_save manually converting all "" into nil.
This really doesn't seem very DRY, and I feel like I must be missing something.
Check out the attribute_normalizer plugin.
There is no notion of "submitting nil" from a browser.
If field of name 'foo' is not submitted, then if you ask for params[:foo], it will return nil, which seems to be the behavior you desire.
Can you explain more what you want to do?