Ruby & Rails notes

This page contains various notes for myself and maybe something helpful for you.

Concepts

Execute around, a way to use ruby blocks to execute boilerplate or helpful code without adding the code to a method. This helps when trying to log around a method without adding the logging code to the method itself.

RVM issues

RVM is plagued with various OS level issues so below are some fixes for OS X:

Autolibs is a feature built into RVM to allow you to automatically install dependencies on your system.

From https://rvm.io/rvm/autolibs

Issue below:

» rvm install ruby-2.4.6 --with-openssl=/usr/local/opt/openssl@1.1/bin/openssl
Warning, new version of rvm available '1.29.10-next', you are using older version '1.29.0'.
You can disable this warning with:    echo rvm_autoupdate_flag=0 >> ~/.rvmrc
You can enable  auto-update  with:    echo rvm_autoupdate_flag=2 >> ~/.rvmrc
ruby-2.4.6 - #removing src/ruby-2.4.6 - please wait
Checking requirements for rvm_pkg.
Warning, you are using 'rvm autolibs rvm_pkg', this is rarely used and can produce errors,
make sure to report any problems to https://github.com/rvm/rvm/issues
Requirements installation successful.
Installing Ruby from source to: /Users/muhammad/.rvm/rubies/ruby-2.4.6, this may take a while depending on your cpu(s)...
ruby-2.4.6 - #downloading ruby-2.4.6, this may take a while depending on your connection...
ruby-2.4.6 - #extracting ruby-2.4.6 to /Users/muhammad/.rvm/src/ruby-2.4.6 - please wait
ruby-2.4.6 - #configuring - please wait
ruby-2.4.6 - #post-configuration - please wait
ruby-2.4.6 - #compiling - please wait
Error running '__rvm_make -j 1',
showing last 15 lines of /Users/muhammad/.rvm/log/1587154276_ruby-2.4.6/make.log
                          ^
num2int.c:64:21: error: expected ')'
    sprintf(buf, "%"PRI_LL_PREFIX"u", NUM2ULL(num));
                    ^
num2int.c:64:5: note: to match this '('
    sprintf(buf, "%"PRI_LL_PREFIX"u", NUM2ULL(num));
    ^
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/secure/_stdio.h:47:27: note: expanded from macro 'sprintf'
  __builtin___sprintf_chk (str, 0, __darwin_obsz(str), __VA_ARGS__)
                          ^
190 warnings and 2 errors generated.
make[2]: *** [num2int.o] Error 1
make[1]: *** [ext/-test-/num2int/all] Error 2
make: *** [build-ext] Error 2
+__rvm_make:0> return 2
There has been an error while running make. Halting the installation.

This breaks if RVM is trying to use autolibs, fix this by first enforcing homebrew as the package manager:

rvm autolibs homebrew

Then use your installation command, in my version:

rvm install ruby-2.4.6 --with-openssl-dir=brew --prefix openssl

Gem install issues

Rugged gem not install

Error below when install rugged 0.28.0

Using rugged 0.28.0 from https://github.com/xxxxxx/rugged.git (at master@58fdb24)
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /Users/muhammad/.rvm/gems/ruby-2.4.6/bundler/gems/rugged-58fdb24b29e3/ext/rugged
/Users/muhammad/.rvm/rubies/ruby-2.4.6/bin/ruby -r ./siteconf20200625-43198-ze19sj.rb extconf.rb
checking for gmake... no
checking for make... yes
checking for cmake... yes
checking for pkg-config... yes
-- Checking prototype qsort_r for HAVE_QSORT_R_BSD - False
-- Checking prototype qsort_r for HAVE_QSORT_R_GNU - False
-- Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the system variable OPENSSL_ROOT_DIR (missing: OPENSSL_INCLUDE_DIR)
-- Found Security /System/Library/Frameworks/Security.framework
-- Found CoreFoundation /System/Library/Frameworks/CoreFoundation.framework
-- Could NOT find HTTP_Parser (missing: HTTP_PARSER_INCLUDE_DIR HTTP_PARSER_LIBRARY)
-- http-parser version 2 was not found or disabled; using bundled 3rd-party sources.
-- Checking for module 'libssh2'
--   Package 'libssl', required by 'libssh2', not found
-- LIBSSH2 not found. Set CMAKE_PREFIX_PATH if it is installed outside of the default search path.
-- Found Iconv: -L/usr/lib -liconv
-- Enabled features:
 * threadsafe, threadsafe support
 * HTTPS, using SecureTransport
 * SHA, using CollisionDetection
 * http-parser, http-parser support (bundled)
 * zlib, using system zlib
 * iconv, iconv encoding conversion support

-- Disabled features:
 * debugpool, debug pool allocator
 * tracing, tracing support
 * SSH, SSH transport support
 * SPNEGO, SPNEGO authentication support

-- Configuring done
-- Generating done
-- Build files have been written to: /Users/muhammad/.rvm/gems/ruby-2.4.6/bundler/gems/rugged-58fdb24b29e3/vendor/libgit2/build
 -- /usr/bin/make
extconf.rb:22:in `sys': ERROR: '/usr/bin/make' failed (RuntimeError)
	from extconf.rb:105:in `block (2 levels) in <main>'
	from extconf.rb:101:in `chdir'
	from extconf.rb:101:in `block in <main>'
	from extconf.rb:98:in `chdir'
	from extconf.rb:98:in `<main>'

To see why this extension failed to compile, please check the mkmf.log which can be found here:

  /Users/muhammad/.rvm/gems/ruby-2.4.6/bundler/gems/extensions/x86_64-darwin-19/2.4.0/rugged-58fdb24b29e3/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /Users/muhammad/.rvm/gems/ruby-2.4.6/bundler/gems/rugged-58fdb24b29e3 for inspection.
Results logged to /Users/muhammad/.rvm/gems/ruby-2.4.6/bundler/gems/extensions/x86_64-darwin-19/2.4.0/rugged-58fdb24b29e3/gem_make.out

An error occurred while installing rugged (0.28.0), and Bundler cannot continue.


In Gemfile:
  rugged

Libarchive and lib-sodium not working on MacOS

This is because libarchive hasn’t gotten an update in years. So you’ll want to use ffi-libarchive which seems to solve the problem when the bundler install fails. And use brew install libsodium for the lib-sodium gem when it’s not working.

Helpful gems

Looping in Ruby

list_of_things.each { |iterator| puts iterator.something } # one-liner

Skipping in loops

3.times do |i|
  puts i
  next if i == 2
end