Search an attribute for a substring [closed] - ruby

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to remove all whitespaces from an object's attribute that contains a given substring. For example, I have an object event and attributes: 4IP2, 3IP5, 2IP1. I would like to do the following:
event[4IP2].gsub(/\s+/, '')
in a generic manner, i.e.,
event[*IP*].gsub(/\s+/, '')
which should work for all attributes 4IP2, 3IP5, 2IP1. Appreciate any help.

Assuming, that event is a hash, here you go:
▶ event = { '4IP2' => 'a b c', '3GG5' => 'ffff f', '2IP1' => 'ggg ' }
▶ event.map { |k, v| [k, /IP/ =~ k ? v.delete(' ') : v] }.to_h
#⇒ { "2IP1" => "ggg", "3GG5" => "ffff f", "4IP2" => "abc" }

If you want to replace the attributes in-place:
event.each {|k,v| v.gsub!(/\s+/, '') if /IP/ =~ k}
Otherwise, to create a copy:
Hash[event.map {|k,v| [k, /IP/ =~ k ? v.gsub(/\s+/, '') : v]}]

Related

Filter hash keys of certain length? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I can't seem to figure out a line of code to be able to do this. I have a hash that I would like to be able to pick out all of the keys that are at least 6 characters long.
Try this one
your_hash.keys.select { |k| k.length >= 6 }
as you want "length of values"
{a: 'carl', b: 'steve'}.map {|k, v| v.size }
# => [4, 5]
# select sizes values directly within the hash enumeration
{a: 'carl', b: 'steve'}.values.map {|v| v.size }
# => [4, 5]
# convert hash to array of values and then select the sizes values
{a: 'carl', b: 'steve'}.values.select {|v| v.size > 4 }
# => ["steve"]
# convert hash to array of values and then select values that has a condition
if you want more advanced topic on "Lazy" Enumeration http://www.eq8.eu/blogs/28-ruby-enumerable-enumerator-lazy-and-domain-specific-collection-objects

create nested hash from a list [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have got a list l = [:foo, :bar, :baz] and want to assign a varible into a hash h ={} programmatically.
Hash should look like
{ foo: { bar: { baz: some_value } } }
Note: the keys are variables!
Question:
How can I do this?
You could use inject on the reversed list :
l = [:foo, :bar, :baz]
h = l.reverse.inject(:some_value) do |value, key|
{ key => value }
end
p h
# {:foo=>{:bar=>{:baz=>:some_value}}}
reverse is used in order to build the innermost hash first, and keep building the nested hash outwards.

How to find partial duplicate in array? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to remove partial duplicate value from an Array.
['John', 'Johnson', 'Mike', 'Tim', 'Timberland']
I want remove partial duplicate value.
in this case, I want keep longer string value.
['Johnson', 'Mike', 'Timberland']
Any good idea?
This is how I would do:
ary = ['John', 'Johnson', 'Mike', 'Tim', 'Timberland']
ary.select {|e| ary.grep(Regexp.new(e)).size == 1 }
# => ["Johnson", "Mike", "Timberland"]
Just do the following, in case when part is resided at the beginning of a word only:
array = ['John', 'Johnson', 'Mike', 'Tim', 'Brakatim', 'Weltimwel']
# => ["John", "Johnson", "Mike", "Tim", "Brakatim", "Weltimwel"]
array.reject {| v | " #{array.join( ' ' )} " =~ /\W#{v}\w/i }
# => ["Johnson", "Mike", "Tim", "Brakatim", "Weltimwel"]
Or in case when part is resided at the beginning of a word, and at the end or middle of it:
array = ['John', 'Johnson', 'Mike', 'Tim', 'Timberland', 'Brakatim', 'Weltimwel']
# => ["John", "Johnson", "Mike", "Tim", "Timberland", "Brakatim", "Weltimwel"]
array.reject {| v | " #{array.join( ' ' )} " =~ /\W#{v}\w|\w#{v}\W|\w#{v}\w/i }
# => ["Johnson", "Mike", "Timberland", "Brakatim", "Weltimwel"]

How to make a copy of an array and manipulate its element in ruby [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have this array which I would like to copy and change an element's value. How can I do it (Ruby 1.9.3p429)
a = Array.new(2,"test") #a => ["test","test"] #a.object_id => 21519600 #a[0].object_id => 21519612
b = a.clone #b => ["test","test"] #b.object_id => 22940520 #b[0].object_id => 21519612
c = a.dup #c => ["test","test"] #c.object_id => 22865176 #c[0].object_id => 21519612
d = Array.new(a) #d => ["test","test"] #c.object_id => 23179224 #d[0].object_id => 21519612
c[0].upcase! #produces #a => ["TEST","TEST"], #b => ["TEST","TEST"], #c => ["TEST","TEST"] ...`
In Ruby every object is actually a reference to object so if you have array
x = [a, b, c, d]
and copy it into another array
y = x.clone
it will copy references to original objects, not objects themselves.
To do exactly what you want you would have to copy objects in a loop, however you're too focused on how you want to achieve array copying, instead of achieving your ultimate goal, get a new array which consists of upcased items of the original array.
Explore the Enumerable module and you will find things like #map, #select, #inject, etc. For instance this is how you get a copy of array with all names upcased:
["test", "test"].map { |element| element.upcase }
From your comment, you seem to want to upcase "c[0] only". I don't understand why you need to capitalize via a duplicate of a, but here is how to do it.
a = Array.new(2){"test"}
c = a.dup
c[0].upcase!
a # => ["TEST", "test"]

extracting non-empty value in ruby [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
pry(main)> s = {:a =>2, :d=>'foo', :x => ' ', :n => true, :z => nil}
=> {:a=>2, :d=>"foo"}
pry(main)> s.each do |k,v| p k unless v.empty? end
NoMethodError: undefined method `length' for 2:Fixnum
I understand it happens because fixnum does not have empty methods. Then how to solve this problem in a slick way, no nasty finding data type first and then check it? I want to print those k where v has some value. Yes true is considered a value, but not bunch of spaces. For me "have value" means non-empty characters and boolean true.
With your updated comments, I think that is what you want.
s = {:a =>2, :d=>'foo', :x => ' ', :n => true, :z => nil}
s.each { |k,v| p(k) if !!v && !v.to_s.strip.empty? }
# :n
# :d
# :a
Quick solution:
s.each {|k,v| p k unless v.to_s.empty?}

Resources