Iterating over a Ruby Hash while tracking the loop index
You know each_with_index
from arrays:
['hello', 'universe'].each_with_index do |value, index|
puts "#{index}: #{value}"
end
# 0: hello
# 1: universe
This also works on hashes. However, mind the required syntax:
{ hello: 'universe', foo: 'bar' }.each_with_index do |(key, value), index|
puts "#{index}: #{key} => #{value}"
end
# 0: hello => universe
# 1: foo => bar
The reason is that each_with_index
yields 2 elements to the block, and you need to deconstruct the first el...
Fixing Ruby debugger: *** Unknown command: "something". Try "help".
So you have placed a breakpoint somewhere and now want to dig around, but not even inspecting variables is working:
(rdb:3) @order_item
*** Unknown command: "@order_item". Try "help".
The reason is, you must tell the debugger to evaluate your expression. One workaround is to call irb
to open an irb session at your breakpoint. Resume by sending Ctrl+D
twice or by returning to the outer irb with "exit
" and then continuing with "c
".
However, the native debugger command for your issue is eval
(or its shorter alias `e...
gazay/ids_please
Parses URLs of social networks to extract IDs or screen names.
It does not get confused by child routes: you may also pass URLs like a user's twitter photo stream and the gem will extract their twitter ID .
Note that it just parses URLs, and does nothing magic like looking up IDs when the URL contains only a screen name (e.g. the Instagram API requires you to send the user ID almost always while you at first only know their screen name).
Spreewald 1.1.0 released
Spreewald 1.1.0 drops the be_true
and be_false
matchers in order to be RSpec 3 and Ruby 2 compatible. For backward compatibility, these matchers are replaced with == true
and == false
.
Defining Ruby strings with % (percent) notation
The attached post shows some alternative ways to define Strings in Ruby using the percent notation. This can be useful when you'd like to use quotes ("
) or single-quotes ('
) in your strings:
%(any alpha-numeric)
%[char can be]
%%used as%
%!delimiter\!! # escape '!' literal
%( (pa(re(nt)he)sis) ) #=> "(pa(re(nt)he)sis)"
%[ [square bracket] ] #=> "[square bracket]"
%{ {curly bracket} } #=> "{curly bracket}"
%< <pointy bracket> > #=> "<pointy bracket>"
%< \<this...
Ruby lets you redefine backticks
This actually works:
class Klass
def initialize
`hi world`
end
def `(message)
puts "Called with backticks: #{message}"
end
end
Klass.new # Prints "Called with backticks: hi world"
Hat tip to @jcoglan.
Use byebug on Ruby 2+
The debugger
gem does not seem to be properly working on Ruby 2. Use byebug
instead!
Byebug is a simple to use, feature rich debugger for Ruby 2. It uses the new TracePoint API for execution control and the new Debug Inspector API for call stack navigation, so it doesn't depend on internal core sources. It's developed as a C extension, so it's fast. And it has a full test suite so it's reliable. Note that byebug works only for ruby 2.0.0 or newer. For...
Rails Assets
Automatically builds gems from Bower packages (currently 1700 gems available). Packaged Javascript files are then automatically available in your asset pipeline manifests.
Why we're not using it
At makandra we made a choice to use bower-rails instead. While we believe Rubygems/Bundler to be superior to Javascript package managers, we wanted to use something with enough community momentum behind it that it won't go away in 10 years...
Working around OpenSSL::SSL::SSLErrors
If your requests blow up in Ruby or CURL, the server you're connecting to might only support requests with older SSL/TLS versions.
You might get an error like: OpenSSL::SSL::SSLError: SSL_connect SYSCALL returned=5 errno=0 state=unknown state
SSL Server Test
This SSL Server Test can help finding out which SSL/TLS versions the server can handle.
Ruby
In Ruby, you can teach Net::HTTP
to use a specific SSL/TLS version.
uri = URI.parse(url)
ssl_options = {
use_ssl: true,
ssl_version...
JavaScript: Calling a function with a variable number of arguments
This card describes how to pass an array with multiple element to a JavaScript function, so that the first array element becomes the first function argument, the second element becomes the second argument, etc.
Note how this is different from passing the entire array as the first argument. Compare these two different ways of calling fun()
in Ruby:
# Ruby
array = [1, 2, 3]
fun(array) # same as fun([1, 2, 3]) (1 argument)
fun(*array) # same as fun(1, 2, 3) (3 arguments)
Depending on your culture the spreading of array eleme...
whenever: Make runner commands use bundle exec
In whenever you can schedule Ruby code directly like so:
every 1.day, :at => '4:30 am' do
runner "MyModel.task_to_run_at_four_thirty_in_the_morning"
end
Combined with the best practice to hide background tasks behind a single static methods you can test, this is probably preferable to defining additional Rake tasks.
Unfortunately when whenever register a runner command, it doesn't use bundle exec
in the resulting crontab. This gets you errors like this:
`gem_original_require': no suc...
shoulda-matcher methods not found in Rails 4.1
So you're getting an error message like the following, although your Gemfile lists shoulda-matchers
and it has always worked:
NoMethodError:
undefined method `allow_value' for #<RSpec::ExampleGroups::Person::Age:0x007feb239fa6a8>
This is due to Rails 4.1 (specifically, Spring) revealing a weak point of shoulda-matchers
-- jonleighton explains why.
Solution
The solution is to follow [the gem's installation guide](https://github.com/thoughtbot/sh...
ruby-concurrency/atomic · GitHub
Provides a value container that guarantees atomic updates to this value in a multi-threaded Ruby program.
Originally linked to:
ruby-concurrency/atomic (Deprecated)
Ruby 2.1 returns a symbol when defining a method
Since Ruby 2.1, defining a method returns its name as a Symbol:
def foo() end # => :foo
define_method :foo do end # => :foo
You can use this to do Python-like decorators like so:
private def foo; end
memoize def foo; end
Ruby 2.0 introduces keyword arguments
"Keyword arguments" allow naming method arguments (optionally setting a default value). By using the double-splat operator, you can collect additional options. Default values for standard arguments still work (see adjective
).
def greet(name, adjective = 'happy', suffix: '!', count: 7, **options)
greeting = options[:letter] ? 'Dear' : 'Hello'
puts "#{greeting} #{adjective} #{name + suffix * count}"
end
Invoke the method like this:
greet('Otto', 'sad', suffix: '??', count: 9, include_blank: true)
In Ruby 2.1+,...
mattheworiordan/capybara-screenshot
Using this gem, whenever a Capybara test in Cucumber, Rspec or Minitest fails, the HTML for the failed page and a screenshot (when using capybara-webkit, Selenium or poltergeist) is saved into $APPLICATION_ROOT/tmp/capybara.
Link via Binärgewitter Podcast (German).
If Guard takes forever to start...
For me guard recently took a very long to start (as in "minutes"), because I had lots of images in public/system
.
Upgrading the listen
gem (which is a dependency) to 2.7.7 fixed this.
assignable_values 0.11.0 can return *intended* assignable values
As you know, assignable_values does not invalidate a record even when an attribute value becomes unassignable. See this example about songs:
class Song < ActiveRecord::Base
belongs_to :artist
belongs_to :record_label
assignable_values_for :artist do
record_label.artists
end
end
We'll create two record labels with one artist each and create a song for one artist. When we change the song's record label, its artist is still valid.
makandra = RecordLabel.create! name: 'makandra records'
dominik...
Things to consider when using Travis CI
Travis CI is a free continuous integration testing service. However, it is really fragile and will break more than it will work.
If you choose to use it anyway, learn the lessons we already learnt:
Use a compatible Rubygems for Rails 2.3 on Ruby 1.8.7
Ruby 1.8.7 is not compatible with current Rubygems versions (> 2.0). Runnig rvm rubygems latest-1.8 --force
will fix this and install Rubygems version 1.8.29.
To make Travis CI do this, add `before_script: rvm rubygems latest-1....
rbenv: How to update list of available Ruby versions on Linux
When you tell rbenv to install a Ruby it does not know about, you will get an error message.
$ rbenv install 2.1.2
ruby-build: definition not found: 2.1.2
You can list all available versions with `rbenv install --list'.
If the version you're looking for is not present, first try upgrading
ruby-build. If it's still missing, open a request on the ruby-build
issue tracker: https://github.com/sstephenson/ruby-build/issues
(Fun fact: Recent versions of ruby-build will give you a more helpful error message which...
EdgeRider 0.3.0 released
EdgeRider 0.3.0 adds support for Rails 4.1 and Ruby 2.1. It forward-ports ActiveRecord::Base.scoped
to Rails 4.1.
How to remove RSpec "old syntax" deprecation warnings
RSpec 3.0 deprecates the :should
way of writing specs for expecting things to happen.
However, if you have tests you cannot change (e.g. because they are inside a gem, spanning multiple versions of Rails and RSpec), you can explicitly allow the deprecated syntax.
Fix
Inside spec/spec_helpber.rb
, set rspec-expectations
’ and/or rspec-mocks
’ syntax as following:
RSpec.configure do |config|
# ...
config.mock_with :rspec do |c|
c.syntax = [:should, :expect]
...