Useful Ruby Pathname method
If you have a Ruby Pathname Show archive.org snapshot , you can use the method :/ Show archive.org snapshot to append filepaths to it.
With this method, Ruby code can look like this:
Rails.root/"features"/"fixtures"/"picture.jpg"
Alternatively you can use the #join
method, which feels less magic:
Rails.root.join('features', 'fixtures', 'picture.jpg')
Related cards:
Using Ruby's Method objects for inspecting methods
Do you remember finding where a method is defined?
I recently learned from a senior colleague that Method objects ar...
Ruby: Finding where a method is defined
Dead simple: Get the method object and ask for its owner:
"foo".method(:upcase)
=> #<Method: String#upcase>
"foo".method(:upcase).owner
=> String
Ruby 1.9 adds a method Method#source_location
that returns file and line number ...
Ruby 1.9 or Ruby 2.0 do not allow using shortcut blocks for private methods
Consider this class:
class Foo
private
def test
puts "Hello"
end
end
While you can say create a block to call that method (using ampersand and colon) on Ruby 1.8, ...
1.8.7 > Foo.new.tap(&:te...
Fix Rubygems binary error: undefined method `activate_bin_path' for Gem:Module (NoMethodError)
So you're getting an error like this:
undefined method `activate_bin_path' for Gem:Module (NoMethodError)
Here is what happened:
- You installed a recent version of Rubygems
- You installed some gems that install a binary (like `bun...
How Ruby method lookup works
When you call a method on an object, Ruby looks for the implementation of that method. It looks in the following places and uses the first implementation it finds:
- Methods from the object's singleton class (an unnamed class that only exists fo...
About Ruby's conversion method pairs
Ruby has a set of methods to convert an object to another representation. Most of them come in explicit and implicit flavor.
explicit | implicit |
---|---|
to_a |
to_ary |
to_h |
to_hash |
Protected and Private Methods in Ruby
In Ruby, the meaning of protected
and private
is different from other languages like Java. (They don't hide methods from inheriting classes.)
private
Private methods can only be called with implicit receiver. As soon as you specify a...
How to fix "undefined method `name' for Array" error when running bundled commands on Ruby 1.8.7 + Rails 2.3
On recent/fresh installations of Ruby 1.8.7 you may encounter this error why calling any bundled binary (or just bundle exec
):
/home/arne/.rvm/gems/ruby-1.8.7-p374@global/gems/rubygems-bundler-1.4.2/lib/rubygems-bundler/noexec.rb:75:in ...
Missing certificates for rubygems and bundler in Ruby 1.8.7
Using Ruby 1.8.7 you will not be able to use the maximum versions Rubygems 1.8.30 and Bundler 1.17.3 with https://rubygems.org/
anymore. This is a result of a server certificate on December 5th, 2020. The resulting errors will...
Finding a method name on a Ruby object
Wondering how a specific method on an object is exactly named? You can use Enumerable#grep
to detect it in the array of methods.
@user.methods.grep /name/ # => [:name, :first_name, :last_name]
You can also call #private_methods
o...