This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
how to check if my array includes an object - rails
I have array
array['one', 'two', 'three']
How i find that 'two' element present in array.
Is any method in ruby which can find this?
Thanks
array.include?('two') returns true or false
http://ruby-doc.org/core/classes/Array.html#M002203
array.include?('two')
you can use:
array.index('two')
will return index of object if present else will return nil.
Related
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
This question already has answers here:
Deleting a hash from array of hashes in Ruby
(2 answers)
Closed 5 years ago.
I have array of hashes in json format, and I have to remove one of the hash from that array, I am iterating that array and if that particular key/ value matches i am deleting that hash,
I found clear() method but, clear leaves the {}, which I don't require
I want the whole hash to be removed
[{"question":"0a2a3452","answer":"lopq"},
{"question":"58deacf9","answer":"admirationo"},
{"question":"32c53e","answer":"acion"},
{"question":"b5546bcf","answer":"figure"},
{"question":"4f246a10","answer":"zelta"},
{"question":"bf546c04","answer":"deltaa"}]
i.e if my key matches as "0a2a3452", I want to delete the first hash
You can do with delete_if method:
arr = [{"question":"0a2a3452","answer":"lopq"},
{"question":"58deacf9","answer":"admirationo"},
{"question":"32c53e","answer":"acion"},
{"question":"b5546bcf","answer":"figure"},
{"question":"4f246a10","answer":"zelta"},
{"question":"bf546c04","answer":"deltaa"}]
arr.delete_if {|a| a[:question] == '0a2a3452' }
Try this:
items = [
{"question":"0a2a3452","answer":"lopq"},
{"question":"58deacf9","answer":"admirationo"},
{"question":"32c53e","answer":"acion"},
{"question":"b5546bcf","answer":"figure"},
{"question":"4f246a10","answer":"zelta"},
{"question":"bf546c04","answer":"deltaa"}
]
items = items.reject {|i| i[:question] == '0a2a3452'}
This question already has an answer here:
Use variable in parameter ruby on rails? [closed]
(1 answer)
Closed 7 years ago.
I want to check if param key exists with a variable name and if it exists I want to use value something like params[filenamestring[-1]].
filenamestring is any array generate with split
If you variable is a hash than here is your answer
if params.has_key?(filenamestring[-1])
param_value = param[filenamestring[-1]]
end
You can check existence of key using has_key? method:
key = filenamestring[-1]
# or, key = filenamestring.last
if params.has_key?(key)
value = params[key]
# do stuff with value
end
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 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