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

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

Related

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.

passing a method to a method I create in Ruby [duplicate]

This question already has answers here:
Call a method by name [duplicate]
(4 answers)
Closed 8 years ago.
I'm trying to pass min and then later max to a method, but it doesn't seem to work.
def worker(x)
[3,4].x
end
worker(min)
Do you want call methods by name? Then, use Object#send.
def worker(x)
[3,4].send(x)
end
worker(:min)
# => 3
worker(:max)
# => 4

What does the !! mean in this Ruby function? [duplicate]

This question already has answers here:
What does !! mean in ruby?
(8 answers)
Closed 9 years ago.
What does the !! mean in this Ruby function?
def is_i?
!!(self =~ /^[-+]?[0-9]+$/)
end
It makes sure the response is a boolean. So nil or false wil become false, any other value becomes true

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

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.

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