How to specify an `#since` for a method alias? - ruby

I am writing a Ruby gem. To document my code, I am using YARD. I have been using #since (see here) to mark in which gem versions methods have been added. However, I'm having a problem. In one version of my gem I had defined a method. Now, in a new version of the gem, I want to define an alias for that method. How, with YARD/RDoc, can I add #since to an alias?
I have tried this, which didn't work:
# ...
# #since 1.0.0
def my_method
# ...
end
# #since 1.1.0
alias new_method my_method
Any ideas?

You should be able to use the #!method directive to accomplish this:
# #since 1.0.0
def my_method
# ...
end
# #!method new_method
# #since 1.1.0
alias new_method my_method

Related

Method name is in list returned by Object#singleton_methods but not accessible using Object#singleton_method

I'm confused with the difference between Object#singleton_method and Object#singleton_methods.
I thought that the result in Object#singleton_methods is the true set of !!Object#singleton_method(:name), but it seems to be different.
Here is the example script:
require "active_support/deprecation"
# [:debug=, :debug]
ActiveSupport::Deprecation.singleton_methods(false).grep(/debug/)
# [:debug=, :debug]
ActiveSupport::Deprecation.singleton_methods.grep(/debug/)
begin
ActiveSupport::Deprecation.singleton_method(:debug) # exception
rescue => e
puts e.backtrace
raise
end
Gemfile is
# frozen_string_literal: true
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
# gem "rails"
gem 'activesupport', '5.1.6'
The result is this:
% bundle install
Fetching gem metadata from https://rubygems.org/..............
Resolving dependencies...
Using concurrent-ruby 1.0.5
Using i18n 1.0.0
Using minitest 5.11.3
Using thread_safe 0.3.6
Using tzinfo 1.2.5
Using activesupport 5.1.6
Using bundler 1.16.1
Bundle complete! 1 Gemfile dependency, 7 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
% bundle exec ruby test.rb
test.rb:8:in `singleton_method'
test.rb:8:in `<main>'
Traceback (most recent call last):
1: from test.rb:8:in `<main>'
test.rb:8:in `singleton_method': undefined singleton method `debug' for `ActiveSupport::Deprecation' (NameError)
I expected that ActiveSupport::Deprecation.singleton_method(:debug) would return #<Method: ActiveSupport::Deprecation.debug>, but raise exception.
I don't understand why. Of course, I know the basic usage in singleton_method and singleton_methods:
# everything is OK!
class Sample; end
class << Sample
def test_method; end
end
# These two expressions behave as I expect.
## [:test_method]
Sample.singleton_methods(false).grep(/test_method/)
## #<Method: Sample.test_method>
Sample.singleton_method(:test_method)
Thanks.
Update: It looks like this is a bug in Ruby. Vasiliy Ermolovich has created an issue along with a patch to address it. The fix has already been merged so this will be resolved in an upcoming update.
This isn't a full answer to your question I'm afraid, but after a bit of digging I've been able to make a minimal example that demonstrates the same thing without the dependency on Active Support. I thought this might still be useful to you for your investigations, or might help someone else who can come along with a complete explanation.
It appears that the unexpected behaviour of singleton_method comes from use of Module.prepend which ActiveSupport::Deprecation uses here.
The same error can be seen with this small example:
module Empty; end
class MyClass
singleton_class.prepend(Empty)
def self.my_method
puts "my method called"
end
end
p MyClass.singleton_methods(:false)
m = MyClass.singleton_method(:my_method)
m.call
Running this gives:
❯ ruby example.rb
[:my_method]
Traceback (most recent call last):
1: from example.rb:11:in `<main>'
example.rb:11:in `singleton_method': undefined singleton method `my_method' for
`MyClass' (NameError)
With the call to prepend removed this runs as you would expect:
❯ ruby example.rb
[:my_method]
my method called

NoMethodError: undefined method `assert_true'

NoMethodError: undefined method `assert_true'
Am getting above error while running tests using test-unit in ruby. test-unit gem and rake versions are below,
test-unit (2.5.5)
rake (10.1.0)
Sample test file:-
require 'test/unit'
class Sample < Test::Unit::TestCase
def setup
# code block
end
def test_sample
assert_true("test"=="test")
end
def teardown
# code block
end
end
How to solve this ?
I solved the problem by using following way. No need to change assert statements.
require 'rubygems'
gem 'test-unit'
require 'test/unit'
class Sample < Test::Unit::TestCase
def setup
# code block
end
def test_sample
assert_true("test"=="test")
end
def teardown
# code block
end
end
assert_true does not appear to be in the list of available assertions for test-unit. Try using assert. Reference: http://ruby-doc.org/stdlib-2.0.0/libdoc/test/unit/rdoc/Test/Unit/Assertions.html
Since 1.9.2, test/unit is a wrapper around minitest, implemented directly in the ruby source code. The assert_true method does not exist in the new implementation, just use assert instead, as Simon Brahan already suggested. So the gem source you were looking at is no longer in use. The now relevant documentation is here.

Rake task occasionally fails, undefined error

I check out a fresh commit in a repo that works for everyone else. I run a rake task, which calls this code and throws an undefined error, even though Gem.source_index appears to be defined.
module Gem
puts "in module Gem"
def self.source_index=(index)
puts "defining the source index"
##source_index = index
end
end
module Rails
class GemDependency < Gem::Dependency
attr_accessor :lib, :source, :dep
def self.add_frozen_gem_path
puts "Oh hi there"
puts "the source index is " + Gem.source_index // ERROR HERE
end
The output is
in module Gem
Oh hi there
rake aborted!
undefined method `source_index' for Gem:Module
What's wrong?
Gem.source_index was deprecated in Ruby 1.9 and removed in Ruby 2.0*. I suspect you're using Ruby 2.0 now, which would give you that exact error.
Note that you do define a setter for it, but not a getter.
(* Technically it's probably tied to a specific version of Rubygems rather than Ruby. A 1.9.3 installation with an upgraded rubygems installation would amount to the same thing.)

Switching my Rails 3.2.12 project to Ruby 2.0.0 fails a test

Switching my Rails 3.2.12 project to Ruby 2.0.0 fails a test:
NoMethodError:
private method `initialize_dup' called for #<Receipt:0x007fe06c809428>
Looks like initialize_dup is now a private method.
What can I do to get my tests to pass when using Rails 3.2.12 and Ruby 2.0.0?
For those who are worried about upgrading to 3.2.13 because of some issues it could bring, I added this in an initializer file:
# Because of the Ruby 2.0 privatizes the method and Rails 3.2.12's use of it
# , and because Rails 3.2.13 (which has a fix) also could potentially
# introduce other bugs, we're adding this patch to fix the issue.
#
# Remove this if the project contains Rails >= 3.2.13
module ActiveModel
class Errors
public :initialize_dup
end
module Validations
public :initialize_dup
end
end
class ActiveRecord::Base
public :initialize_dup
end
It been released please use Ruby 2.0.0 with Rails 3.2.13.rc2 and also fixed the above issue initialize_dup in this released and few more fixes.
http://weblog.rubyonrails.org/2013/3/7/Rails-3-2-13-rc2-has-been-released/
It works for me.

ClientSideValidations.formBuilders[settings.type] is undefined

I'm trying to use client_side_validations gem, but have some problems.
First, I have in my Gemfile
gem 'client_side_validations', :git => 'https://github.com/bcardarella/client_side_validations.git'
and I after bundle install I see
Using client_side_validations (3.2.0.beta.2) from https://github.com/bcardarella/client_side_validations.git (at master)
but I don't see this gem in gem list....This is first problem.
Second, and more important, validations do not work. I have an error in firebug console
ClientSideValidations.formBuilders[settings.type] is undefined
and no validations happen. I'm using rails 3.2.1, formtastic 2.0.2 and here is my client_side_validations.rb file
# ClientSideValidations Initializer
# Uncomment the following block if you want each input field to have the validation messages attached.
# ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
# unless html_tag =~ /^<label/
# %{<div class="field_with_errors">#{html_tag}<label for="#{instance.send(:tag_id)}" class="message">#{instance.error_message.first}</label></div>}.html_safe
# else
# %{<div class="field_with_errors">#{html_tag}</div>}.html_safe
# end
# end
Any help will be appriciated.....
The problem was that since Formtastic 2.0 version for correct working client_side_validations gem you need to install client_side_validations-formtastic gem.

Resources