Tag: ruby

  • Execute code if a script directly called

    I often want to run some arbitrary code if I run a script directly. The primary example that comes to mind these days is: Strangely enough, despite primarily using Ruby on a daily basis, I haven’t really thought about how this test looks in Ruby: Bash seemed a little more obvious, but apparently there’s a…

  • Conditional gems in ruby without breaking Gemfile.lock

    Do you want ruby/bundler to skip a gem install on a certain platform, but any other platform needs that gem? The obvious answer would be the following, which would result in also clobbering your Gemfile.lock if bundling on macOS: gem “openssl”, “= 3.2.0” if RUBY_PLATFORM !~ /darwin/ But bundler has its own method, install_if , to accomplish such a…

  • Handling keyword arguments in method_missing in Ruby

    If you implement a method_missing method to receive methods that receive keyword arguments and you only receive and forward (to send) the method name, *args, and &block, you will receive ArgumentError: wrong number of arguments (given 3, expected 2) when the arguments are forwarded. This is because the keyword arguments will revert to the backward-compatible…