extracting non-empty value in ruby [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 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?}

Related

Ruby combine hashes? [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 7 years ago.
Improve this question
Is there a method in ruby to combine two hashes into one? Specifically, given A = {:a => :b} and B = {:b => :c} I want
AB = combine(A,B)
=> {:a => :c}
I can make my own if there isn't one in ruby's standard library but I'd rather not reinvent the wheel.
a = {:a => :b}
b = {:b => :c}
# Works on ruby >= 2.1
c = a.map{|k, v| [k, b[v]]}.to_h #=> {:a => :c}
# Works on all versions of ruby
c = Hash[a.map{|k, v| [k, b[v]]}] #=> {:a => :c}

Search an attribute for a substring [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 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]}]

How to iterate through this array within an array to see if all values are equal? [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
Here is my sample code
:key1 => "a"
:key2 => "b"
:key3 => "c"
array1 = [[:key1, :key1, :key1],[:key1, :key2, :key3],[:key2, :key2, :key1]]
array1.each { |x| if x.sym_tos == "a"
puts "All match!"
else
puts "no match"
end
}
Yet when I run it, I get the following error code:
undefined method `sym_to_s' for [:R1C1, :R1C2, :R1C3]:Array (NoMethodError)
You probably wanted to say
if x.uniq.length == 1

prime number test in Ruby [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 have an array a = [1,2,3,4,5]. I want to test which of the numbers are prime and wanted to produce the output {1=>false, 2=>true, 3=>true, 4=>false, 5=>true}.
Any one liner will be appreciated.
I posted this as a comment.
require 'prime'
a = (1..5).to_a
Hash[a.map{ |x| [x, x.prime?] }]
=> {1=>false, 2=>true, 3=>true, 4=>false, 5=>true}
The below will work for you,using Prime#prime?:
require 'prime'
a = [1,2,3,4,5]
Hash[a.zip(a.map(&Prime.method(:prime?)))]
# => {1=>false, 2=>true, 3=>true, 4=>false, 5=>true}
An alternate solution:
a = [1,2,3,4,5]
results = {}
a.each {|i| results[i] = (1..i).map{|x| i/x.to_f % 1 == 0}.count(true) == 2}
puts results.inspect #=> {1 => false, 2 => true, 3 => true, 4 => false, 5 => true}

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"]

Resources