This question already has answers here:
How do I remove blank elements from an array?
(21 answers)
Closed 4 years ago.
How do I get rid of whitespaces and blank items in an array
names = ["alice", "", "bob", " ", "yankee"]
desired result should be
["alice", "bob", "yankee"]
In rails is simple, use present?
names.select(&:present?)
If you try this below it will provide you with a new array assigned to newNames that does not include the empty elements.
newNames = names.reject { |n| n.empty? }
Related
This question already has an answer here:
Ruby Array Initialization [duplicate]
(1 answer)
Closed 3 years ago.
Recently I was playing with arrays and found out a weird behavior. I created a new array using Array.new.
arr = Array.new(4,"happy")
["happy", "happy", "happy", "happy"]
I appended a word into the second element in that array like shown below
arr[1] <<" Dino"
When I looked at arr, I was expecting an array with the second element having an appended word. But to my surprise array returned with all elements with the appended word.
["happy Dino", "happy Dino", "happy Dino", "happy Dino"]
How can this happen? Are we creating copies of the same string when we are creating the array? It doesn't happen if we use arr[1]= " Dino". Somebody can explain why this is happening?
Yes, you a right. See the ruby docs
When a size and an optional default are sent, an array is created with size copies of default. Take notice that all elements will reference the same object default.
You can initialize array in a way like this:
arr = Array.new(4){ 'happy' }
When we take Array.new(4, "Happy") it will create 4 elements with same object id. We can observe in irb arr[1].object_id => 2880099 , arr[2].object_id => 2880099 and remaining 2 elements also will return same object id.
So when we try like arr[1] << "Something" , this string will append to all the same object id's.
Now if we take another element for arr like arr.push("newString"). Now if see the last element object id arr.last.object_id => 4889988,it's different now.
So if we try same command arr[1] << "Something" it won't be effect to newly inserted element because object id is different.
if you don't want this behaviour the use Array.new(4){ 'String' }
This question already has answers here:
Sort Dictionary by keys
(16 answers)
Closed 5 years ago.
I have a dictionary with keys in format: [1:ABC, 113:NUX, 78:BUN, 34:POI, 223:NTY]
When I sorted the array of keys, i get the sorted key array as: [1:ABC, 113:NUX, 223:NTY, 34:POI, 78:BUN]
But I want the sorted array as: [1:ABC, 34:POI, 78:BUN, 113:NUX, 223:NTY]
what am I missing here? what additional sort should I add?
* I am using Swift 2
i got the answer:
let arrSortedKeys = keys.sort{$0.localizedStandardCompare($1) == NSComparisonResult.OrderedAscending}
This question already has answers here:
Rails: How to find_by a field containing a certain string
(5 answers)
Closed 7 years ago.
I'm looking for a way to find all entries in a database containing a specific string. I know it's possible to iterate over every entry and see if a string matches but with a growing database i'd rather not do that. I'm wondering if there is a Rails way of doing it.
something like
Table.where("content containsWord ?", "string")
So a function find all records containing a specific string.
Does anyone know a way to do this?
update
duplicate of Rails: How to find_by a field containing a certain string
You can try this
search_term = "string"
tables = Table.where("tables.content LIKE ?", "%#{search_term}%")
or, if you want to ignore case sensitiveness, then do this
search_term = "string"
tables = Table.where("lower(content) LIKE lower(?)", "%#{search_term}%")
Hope this helps!
This question already has answers here:
Ruby ampersand colon shortcut [duplicate]
(2 answers)
What does map(&:name) mean in Ruby?
(17 answers)
Closed 9 years ago.
Here's the line of code I'm trying to wrap my head around:
Category.all.map(&:id).each { |id| Category.reset_counters(id, :products) }
Hoping someone can help me understand what (&:id) is doing and how it impacts the rest of the line? I believe it turns the symbol :id into a proc that'll respond to id?!? But then it gets confusing...
Thanks in advance!
Category.all.map(&:id)
is shorthand for
Category.all.map { |a| a.id }
as for how it affects the rest of the line, the above section returns all id values as a single Array. This Array of ids is then passed into another call to each, which iteratively passes each id into reset_counters.
This question already has answers here:
How to determine if one array contains all elements of another array
(8 answers)
Closed 9 years ago.
I am trying to compare two Ruby arrays to verify that all of the first array's elements are included in the second one. (The reverse isn't needed.)
For example:
a = ["hello", "goodbye"]
b = ["hello", "goodbye", "orange"]
This should return true.
However, I can't figure out a method that will allow me to do this. Any help would be appreciated!
Many ways are there to check the same :
a = ["hello", "goodbye"]
b = ["hello", "goodbye", "orange"]
(a - b).empty? # => true
a.all?{|i| b.include? i }
# => true
a = ["hello", "welcome"]
b = ["hello", "goodbye", "orange"]
(a - b).empty? # => false
a.all?{|i| b.include? i }
# => false
Array set logic is nice here:
a & b == a
a & b produces a new array consisting of the elements that exist in both a and b. You can then test it against a to make sure that the cross section contains all of the elements of a itself. See the manual entry on Array#& for more details.