Ruby: How to camelize a string with a lower-case first letter
If you want to do JavaScript-style camelization, ActiveSupport's String#camelize
method can actually help you out. Simply pass a :lower
argument to it.
>> 'foo_bar_baz'.camelize
=> "FooBarBaz"
>> 'foo_bar_baz'.camelize(:lower)
=> "fooBarBaz"
Related cards:
Ruby: Replacing Unicode characters with a 7-bit transliteration
Sometimes you need to remove high Unicode characters from a string, so all characters have a code point between 0 and 127. The remaining 7-bit-encoded characters ("Low-ASCII") can be ...
Ruby: How to load a file with a known encoding
In case Ruby does not detected the expected encoding of a file automatically you can specify the known encoding manually.
Example with File.open
file = File.open('some.bin', encoding: Encoding::ASCII_8BIT)
text = file.read
text.encoding =>...
Ruby: How to connect to a host with expired SSL certificate
If you need to make an HTTPS connection to a host which uses an expired certificate, do not disable certificate verifications entirely. Doing that enables e.g. man in the middle attacks.
If you accept only a single expired and known certifica...
How to: Benchmark an Active Record query with a Ruby script
Recently I needed to benchmark an Active Record query for performance measurements. I wrote a small script that runs each query to benchmark 100 times and calculates the 95th percentile.
Note: The script requires sudo permissions to drop RAM cach...
How to make changes to a Ruby gem (as a Rails developer)
At makandra, we've built a few gems over the years. Some of these are quite popular: spreewald (> 1M downloads), active_type (> 1M downloads), and geordi (> 200k downloads)
Developing a Ruby gem is different from developing Rails applications, w...
How to get a backtrace if rspec (or any other ruby process) hangs with no output
If rspec hangs with no output and you dont get a backtrace neither with --backtrace
nor by just killing it with crtl-c,
you can put the following in your spec/spec_helper.rb
:
puts "rspec pid: #{Process.pid}"
trap 'USR1' do
thread...
How to query GraphQL APIs with Ruby
While most Rails Apps are tied to at least one external REST API, machine-to-machine communication via GraphQL is less commonly seen. In this card, I'd like to give a quick intro on how to query a given GraphQL API - without adding any additional ...
Ruby: How to collect a Hash from an Array
There are many different methods that allow mapping an Array to a Hash in Ruby.
Array#to_h
with a block (Ruby 2.6+)
You can call an array with a block that is called with each element. The block must return a [key, value]
tuple.
This i...
Ruby: Fixing strings with invalid encoding and converting to UTF-8
When dealing with external data sources, you may have to deal with improperly encoded strings.
While you should prefer deciding on a single encoding with the data-providing party, you can not always force that on external sources.
It gets worse wh...
Ruby: Comparing a string or regex with another string
In Rubocop you might notice the cop Style/CaseEquality
for e.g. this example:
def foo(expected, actual)
expected === actual
end
In case expected
is a Regex, it suggests to change it to the following pattern:
def foo(expected, ...