This initially seemed like a good idea to me, but after playing with
it there's some issues.  The real problem isn't whether there's a sub
method on Symbol, but that instance_methods used to return Strings in
Ruby 1.8.7 and now returns Symbols in 1.9.  So by monkey patching
symbols to convert to a string to do sub and then return a symbol,
we're solving the problem with the specs not running, but not solving
the real issue, which is that asking if an instance method is defined
will fail between different versions of Ruby because one returns a
list of strings and the other a list of symbols.

Turns out there's already a method for determining whether a method is
defined instead of manually checking in a list: method_defined?

http://ruby-doc.org/core/classes/Module.html#M000452
http://ruby-doc.org/core-1.8.7/classes/Module.html#M000178

This gets around the issue of doing
"instance_methods.include?(:string_or_symbol_here)" not working
between different versions of Ruby since method_defined? accpes
strings or symbols.  In just a sec I'll be sending out a patch doing
this.

On Tue, May 10, 2011 at 10:36 AM, Alex Sharp <ajsh...@gmail.com> wrote:
> Ruby 1.9 removed Symbol#sub, and it's used in various places
> in the puppet codebase.
>
> This is a very simple patch that converts the symbol to a
> string, performs the #sub on the string and then reconverts
> the string back to a symbol. Crude, but it should work. Still,
> once the specs are running under 1.9, we should try to find
> a non-monkeypatch solution
>
> Signed-off-by: Alex Sharp <ajsh...@gmail.com>
> ---
> Local-branch: ticket/2.7.x/7291
>  lib/puppet/util/monkey_patches.rb     |    4 ++++
>  spec/unit/util/monkey_patches_spec.rb |    6 ++++++
>  2 files changed, 10 insertions(+), 0 deletions(-)
>
> diff --git a/lib/puppet/util/monkey_patches.rb 
> b/lib/puppet/util/monkey_patches.rb
> index bd954c6..76ad39d 100644
> --- a/lib/puppet/util/monkey_patches.rb
> +++ b/lib/puppet/util/monkey_patches.rb
> @@ -110,4 +110,8 @@ class Symbol
>   def to_proc
>     Proc.new { |*args| args.shift.__send__(self, *args) }
>   end unless method_defined? :to_proc
> +
> +  def sub(*args)
> +    self.to_s.sub(*args).to_sym
> +  end unless method_defined? :sub
>  end
> diff --git a/spec/unit/util/monkey_patches_spec.rb 
> b/spec/unit/util/monkey_patches_spec.rb
> index 4b609ad..3685a47 100755
> --- a/spec/unit/util/monkey_patches_spec.rb
> +++ b/spec/unit/util/monkey_patches_spec.rb
> @@ -53,3 +53,9 @@ describe "Array#combination" do
>     [1,2,3,4].combination(3).to_a.should == [[1, 2, 3], [1, 2, 4], [1, 3, 4], 
> [2, 3, 4]]
>   end
>  end
> +
> +describe "Symbol#sub" do
> +  it "replaces the contents of the matched symbol with replacement string" do
> +    :replace_me_bro.sub("bro", "dude").should == :replace_me_dude
> +  end
> +end
> \ No newline at end of file
> --
> 1.7.2.3
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Puppet Developers" group.
> To post to this group, send email to puppet-dev@googlegroups.com.
> To unsubscribe from this group, send email to 
> puppet-dev+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/puppet-dev?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to puppet-dev@googlegroups.com.
To unsubscribe from this group, send email to 
puppet-dev+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to