Checking if any element of an array satisfies a condition [duplicate] - ruby

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
check if value exists in array in Ruby
I have this method which loops through an array of strings and returns true if any string contains the string 'dog'. It is working, but the multiple return statements look messy. Is there a more eloquent way of doing this?
def has_dog?(acct)
[acct.title, acct.description, acct.tag].each do |text|
return true if text.include?("dog")
end
return false
end

Use Enumerable#any?
def has_dog?(acct)
[acct.title, acct.description, acct.tag].any? { |text| text.include? "dog" }
end
It will return true/false.

Related

What is this in Ruby? ||= [duplicate]

This question already has answers here:
What does ||= (or-equals) mean in Ruby?
(23 answers)
Closed 6 years ago.
While programming routes in Sinatra, I came across code listed as this:
before do
session[:lists] ||= []
end
What is this operation doing ||= []?
x ||= value is a way to say "if x contains a falsey value, including
nil, assign value to x"
That's setting session[:lists] equal to [] if session[:lists] is falsey.
Related to https://stackoverflow.com/a/6671466/4722305.
It sets [] to session[:lists] when it's nil or falsy
Read more here
;)

How to convert string to UpperCamelCase in Ruby? [duplicate]

This question already has answers here:
Converting string from snake_case to CamelCase in Ruby
(10 answers)
Closed 6 years ago.
I want to turn string into CamelCase fashion In Ruby. The question also applies to words with underscores.
For example:
"human" => "Human"
"little_human" => "LittleHuman"
How can I do this?
With regexp:
def camelize(str)
str.gsub(/(^.)|(_.)/) { |l| l[-1].upcase }
end
In rails there is a camelize method. In ruby you can write the method on your own. Something like
def camelize(s)
s.downcase.split('_').map(&:capitalize).join
end

How should I setup this if and else statement? [duplicate]

This question already has answers here:
Array that alphabetizes and alternates between all caps
(5 answers)
Closed 7 years ago.
Without changing any of the other code- What can I put into my if else statement to make array 0, 2, 4 show up as all capital letters? I can't figure out how to assign this within the if and else statement.
test = []
puts "Please type 5 different words when you're ready-"
5.times do
test << gets.chomp
end
test.sort.each do |user|
if #what could I put in this statement for all capital letters on [0,2,4]?
user.upcase
else #.downcase isn't needed, if its simpler to remove it that is fine
user.downcase
end
end
I think what you need to do is change the each loop to each_with_index. The each_with_index loop should get what you need.
test.sort.each_with_index do |user, index|
if ([0,2,4]include? index)
user.upcase
else
user.downcase
end
end
If the array is going to expand, you could use the #even? method on the index as the check in the if statement like so.
test.sort.each_with_index do |user, index|
if (index.even?)
user.upcase
else
user.downcase
end
end
If you can't change the type of loop then you could use the #index method on the array, to tell you where the user is in the test array like so
test.sort.each do |user|
if ([0,2,4].include? (test.sort.index(user)))
user.upcase
else
user.downcase
end
end
Why without changing any of the other code? Just do this, no one will care:
puts "Please type 5 different words when you're ready-"
5.times.map { |i| i.even? ? gets.chomp.downcase : gets.chomp.upcase }
The times loop autoindexes for this reason, and the ternary operator (?:) is built for this case.

What does "||=" mean in Ruby? [duplicate]

This question already has answers here:
What does ||= (or-equals) mean in Ruby?
(23 answers)
Closed 7 years ago.
I'm still pretty green when it comes to Ruby and am trying to figure out what this is doing:
command_windows.each {|window| window.hidden ||= window.open? }
The command_windows variable appears to be an array of objects. If someone could explain to me what this line of code means, particularly what the ||= symbol is I would appreciate it.
foo ||= "bar" is the equivalent of doing foo || foo = "bar".
As Mischa explained, it checks for a falsy value before assigning.
In your case, you could think of it as:
command_windows.each {|window| window.hidden || window.hidden = window.open? }
which is another way of saying
command_windows.each {|window| window.hidden = window.open? unless window.hidden }
The ||= operator is used to assign new value to variable. If something was assigned to it before it won't work. It is usually used in hashes, so you don't have to check, if something is already assigned.

Ruby hash/sub-hash existance check [duplicate]

This question already has answers here:
How to avoid NoMethodError for missing elements in nested hashes, without repeated nil checks?
(16 answers)
Closed 7 years ago.
I find myself needing to put guards like this:
if hash[:foo] && hash[:foo][:bar] && hash[:foo][:bar][:baz]
puts hash[:foo][:bar][:baz]
end
I'd like to shorten this in some way; I know I can wrap in a begin/rescue block but that seems worse. Maybe something like:
ruby Hash include another hash, deep check
Something like:
def follow_hash(hash, path)
path.inject(hash) { |accum, el| accum && accum[el] }
end
value = follow_hash(hash, [:foo, :bar, :baz])
puts value if value
I found this article very informative: http://avdi.org/devblog/2011/06/28/do-or-do-not-there-is-no-try/
value = Maybe(params)[:foo][:bar][:baz][:buz]

Resources