salesking/king_dtaus
DTAUS & DTAZV are formats for German bank transfers and is short for "Datenträgeraustausch". The format itself totally sucks because it was established in the last century, to be used on floppy disks. Still almost all German banks use it (they only seem innovative at robbing), and it is therefore supported in common banking programs too.
This gem saves you all the trouble when generating DTAUS- or DTAZV-text.
Navigating through the browser history in a cucumber feature using selenium
In order to navigate through the browser history. you can manipulate the window.history object via javascript like follows:
When /^I go back in the browser history$/ do
page.evaluate_script('window.history.back()')
end
For further functions of the window and history objects check out this link.
An improved version of this step is now part of our gem spreewald on Github.
Authorize allowed values with assignable_values
All our projects have enum-like requirements like this:
- An attribute value must be included in a given set of values.
- The list of allowed values must be retrievable in order to render
<select>
boxes. - Each value has a humanized label.
- Sometimes there is a default value.
Most of the time, this requirement is also needed:
- The list of assignable values depends on the user who is currently signed in.
In our past projects there are many different solutions for these related requirements, e.g. ChoiceTrait
, methods like `available_...
Convert primitive Ruby structures into Javascript
Controller responses often include Javascript code that contains values from Ruby variables. E.g. you want to call a Javascript function foo(...)
with the argument stored in the Ruby variable @foo
. You can do this by using ERB tags (<%= ruby_expression %>
) or, in Haml, interpolation syntax (#{ruby_expression}
).
In any case you will take care of proper quoting and escaping of quotes, line feeds, etc. A convenient way to do this is to use Object#json
, which is defined for Ruby strings, numb...
How to fix Passenger "Unexpected end-of-file detected" error
This is for you if Passenger gives you the following useless error message.
Passenger encountered the following error:\
The application spawner server exited unexpectedly: Unexpected end-of-file detected.
- Exception class:
- PhusionPassenger::Rack::ApplicationSpawner::Error
Most often this happens because you are missing a gem. Usually Passenger would tell you about that but in some cases it can't.
To resolve this issue, run:
bundle install
If this does not do the trick for you, take a look at the Apache log files for de...
Building Gem 'RedCloth' with Bundler and GCC 4.6
If you cannot install the gem 'RedCloth' via bundle install you might want to try the following.
Specifying extra build options for bundler which will only be applied when building RedCloth:
bundle config build.RedCloth --with-cflags=-w
Shell script to deploy changes to production and not shoot yourself in the foot
Geordi, our collection of command line tools, has been extended by another command deploy-to-production
. This script encapsulates the following workflow:
- Pull the production branch.
- Show which commits from the master would make it to production with this deploy.
- Ask if you want to proceed.
- If yes, merge the master into the production branch, push and deploy with
bundle exec cap production deploy:migrations
The script will ask you for the names of your master branch, production branch an...
Fix warning: Cucumber-rails required outside of env.rb
After installing Bundler 1.1 you will get the following warning when running tests:
WARNING: Cucumber-rails required outside of env.rb. The rest of loading is being defered until env.rb is called.\
To avoid this warning, move 'gem cucumber-rails' under only group :test in your Gemfile
The warning is misleading because it has nothing to do with moving cucumber-rails
into a :test
group. Instead you need to change your Gemfile
to say:
gem 'cucumber-rails', :require => false
Inspecting a live Ruby process
How to get a backtrace from a running Ruby process:
Ruby 2.6
# First, find out the PID of your Ruby process (e.g. passenger-status)
$ sudo gdb -p PID
(gdb) call rb_eval_string("$stderr.reopen('/tmp/ruby-debug.' + Process.pid.to_s); $stderr.sync = true") # redirects stderr
(gdb) call rb_backtrace() # prints current backtrace to /tmp/ruby-debug.xxx
Stop the process afterwards, since stderr is now borked.
It is possible you have to call rb_backtrace()
multiple times to get the full stacktrace.
Previous method on Ruby 2....
Ruby blocks: Braces and do/end have different precedence
TL;DR {}
binds stronger than do … end
(as always in Ruby, special characters bind stronger than words)
Demo
✔️ Right way
names = ['bRUce', 'STaN', 'JOlIE']
# Blocks in braces are passed to the rightmost method
print names.map { |name| name.downcase }
print(names.map do |name| name.downcase end) # equivalent
=> ["bruce", "stan", "jolie"]
❌ Wrong way
Avoid the examples below, as you pass at least one block to print and not to the enumerator.
names = ['bRUce', 'STaN', 'JOlIE']
# Blocks in do…end ar...
Use the Ruby debugger on Rails 2 script/runner scripts
This card needs to be updated for Rails 3+.
Since there is no --debugger
flag you need to run:
rdebug script/runner lib/scripts/something.rb
That will open an debugging IRB right away, like this:
require File.dirname(__FILE__) + '/../config/boot'
(rdb:1) _
Enter c
to continue and reach your actual debugger
call. Then, debug away.
If nothing happens for you: Make sure ruby-debug
is available in the Gemfile and you require
it.
Ruby: Making your regular expressions more readable with /x and alternative delimiters
The following two hints are taken from Github's Ruby style guide:
If your regular expression mentions a lot of forward slashes, you can use the alternative delimiters %r(...)
, %r[...]
or %r{...}
instead of /.../
.
%r(/blog/2011/(.*))
%r{/blog/2011/(.*)}
%r[/blog/2011/(.*)]
If your regular expression is growing complex, you can use the /x
modifier to ignore whitespace and comments:
regexp = %r{
start # some text
\s # white space char
(group) ...
Ruby: Find the most common string from an array
This will give you the string that appears most often in an array:
names = %w[ foo foo bar bar bar baz ]
names.group_by(&:to_s).values.max_by(&:size).try(:first)
=> "bar"
This is very similar to the linked StackOverflow thread, but does not break on empty arrays.
Note that try
is provided by ActiveSupport (Rails). You could explicitly load activesupport
or use andand
on plain Ruby.
Change how Capybara sees or ignores hidden elements
Short version
- Capybara has a global option (
Capybara.ignore_hidden_elements
) that determines whether Capybara sees or ignores hidden elements. - Prefer not to change this global option, and use the
:visible
option when callingpage.find(...)
. This way the behavior is only changed for this onefind
and your step doesn't have confusing side effects. - Every Capybara driver has its own notion of "visibility".
Long version
Capybara has an option (Capybara.ignore_hidden_elements
) to configure the default...
Connecting the "sequel" gem to MSSQL via ODBC
After you configured your ODBC describe in
- Fix [RubyODBC]Cannot allocate SQLHENV when connecting to MSSQL 2005 with Ruby 1.8.7. on Ubuntu 10.10
- and Connecting to MSSQL with Ruby on Ubuntu - lambie.org
you can connect with sequel
:
require "rubygems"
require "se...
Fix [RubyODBC]Cannot allocate SQLHENV when connecting to MSSQL 2005 with Ruby 1.8.7. on Ubuntu 10.10
I followed this nice guide Connecting to MSSQL with Ruby on Ubuntu - lambie.org until I ran in the following errors:
irb(main):001:0> require "dbi"; dbh = DBI.connect('dbi:ODBC:MyLegacyServer', 'my_name', 'my_password')
DBI::DatabaseError: INTERN (0) [RubyODBC]Cannot allocate SQLHENV
from /usr/lib/ruby/1.8/dbd/odbc/driver.rb:36:in `connect'
from /usr/lib/ruby/1.8/dbi/handles/driver.rb:33:in `connect'
from /usr/lib/ruby...
Ruby, Ruby on Rails, and _why: The disappearance of one of the world’s most beloved computer programmers
Nice article to educate your non-geek girlfriend/boyfriend about the joys of programming.
Make your Rails console (and irb) output better readable
Pour color on your Rails console with awesome_print. Turn confusing long strings into formatted output. Have objects and classes laid out clearly whenever you need it.
Put gem 'awesome_print', :group => :development
into your Gemfile. Now on the Rails console you have the command ap
that will give you a colored, formatted output of whatever you pass it. See the example output of the User
class below.
For customization visit the repository on Github.
 with native extensions
/usr/local/lib/site_ruby/1.8/rubygems/installer.rb:483:in `build_extensions': ERROR: Failed to build gem native extension. (Gem::Installer::ExtensionBuildError)
/usr/bin/ruby1.8 extconf.rb
checking for curl/curl.h in /opt/local/include,/opt/local/include/curl,/usr/include/curl,/usr/include,/usr/include/curl,/usr/local/include/curl... no
need libcurl
You can fix it by installing the libcurl3-dev
package:
sudo apt-get install libcurl3-dev
Now you should...
Setup or update Passenger to use Ruby Enterprise
- Your current
ruby
must be Ruby Enterprise. gem install passenger
passenger-install-apache2-module
- Edit your
httpd.conf
according to the instructions provided at the end of the setup script. - Restart Apache:
sudo service apache2 restart
This also works when you previously ran your Passenger using MRI. Just run the setup as described.
Gherkin: Error during installation
When trying to install the gherkin gem, you might encounter an error with the following lines:
ERROR: Error installing gherkin:
ERROR: Failed to build gem native extension.
...
checking for main() in -lc... yes
creating Makefile
...
cc1: all warnings being treated as errors
Makefile:150: recipe for target 'gherkin_lexer_ar.o' failed
make: *** [gherkin_lexer_ar.o] Error 1
...
If upgrading is not an option, configure build options for gherkin
:
bundle config --local build.gherkin --with-cflags=-w
Your .bundle/config
fi...
Compare two XML strings as hashes
Let's say you have two XML strings that are ordered differently but you don't care about the order of attributes inside containers:
a = '<?xml version="1.0" encoding="UTF-8"?><Authenticate><User>batman</User><Password>secret</Password></Authenticate>'
b = '<?xml version="1.0" encoding="UTF-8"?><Authenticate><Password>secret</Password><User>batman</User></Authenticate>'
Working with plain string comparison is not helpful, of course:
a == b
=> false
Instead, you can use the Nori gem ...