<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: methods code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 24 Jul 2008 04:17:36 GMT</pubDate>
    <description>DZone Snippets: methods code</description>
    <item>
      <title>Optimization - Minimum - Search and Golden Rule</title>
      <link>http://snippets.dzone.com/posts/show/5650</link>
      <description>&lt;code&gt;&lt;br /&gt;from math import sin&lt;br /&gt;def f(x):&lt;br /&gt;    return ((x**4))&lt;br /&gt;&lt;br /&gt;def optMinSearch(f, xi, h, t=1.0e-9):&lt;br /&gt;    ssx = xi&lt;br /&gt;    x = xi&lt;br /&gt;    fx = f(x)&lt;br /&gt;    sx = x&lt;br /&gt;    x += h&lt;br /&gt;    fxn = f(x)&lt;br /&gt;    n = 0&lt;br /&gt;&lt;br /&gt;    while abs(fxn-fx) &gt; t :&lt;br /&gt;        while fx &gt; fxn:&lt;br /&gt;            n += 1&lt;br /&gt;            fx = fxn&lt;br /&gt;            ssx = sx&lt;br /&gt;            sx = x&lt;br /&gt;            x += h&lt;br /&gt;            fxn = f(x)&lt;br /&gt;    &lt;br /&gt;        xx = x&lt;br /&gt;        h = h / 2&lt;br /&gt;        x = ssx&lt;br /&gt;        fx = f(x)&lt;br /&gt;        sx = x&lt;br /&gt;        x += h&lt;br /&gt;        fxn = f(x)&lt;br /&gt;        n += 1&lt;br /&gt;    &lt;br /&gt;    &lt;br /&gt;    return x, fxn, n, sx, xx&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;    &lt;br /&gt;&lt;br /&gt;def optMinGold(f, xi, xf, t=1.0e-9):&lt;br /&gt;    # constants&lt;br /&gt;    A = 0.6180339887&lt;br /&gt;    &lt;br /&gt;    n = 0&lt;br /&gt;    &lt;br /&gt;    x1 = xi&lt;br /&gt;    x2 = xf&lt;br /&gt;&lt;br /&gt;    x3 = x1 + A * (x2-x1)&lt;br /&gt;    x4 = x2 - A * (x2-x1)&lt;br /&gt;    &lt;br /&gt;    fx3 = f(x3)&lt;br /&gt;    fx4 = f(x4)&lt;br /&gt;    &lt;br /&gt;    while abs(x2-x1) &gt; t:&lt;br /&gt;        #print n, x1, x2, abs(x2-x1)&lt;br /&gt;        n+=1&lt;br /&gt;        if fx3 &lt; fx4:&lt;br /&gt;        # fx4 &gt; fx3&lt;br /&gt;            x1 = x4&lt;br /&gt;            x4 = x3&lt;br /&gt;            x3 = x1 + A * (x2-x1)&lt;br /&gt;        else:&lt;br /&gt;         # fx4 &lt; fx3&lt;br /&gt;            x2 = x3&lt;br /&gt;            x3 = x4&lt;br /&gt;            x4 = x2 - A * (x2-x1)&lt;br /&gt;        &lt;br /&gt;        fx3 = f(x3)&lt;br /&gt;        fx4 = f(x4)&lt;br /&gt;        &lt;br /&gt;    return (x1,x2,n)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;xmin, fmin, n, sx, xx = optMinSearch(f, -2.0, 0.1)&lt;br /&gt;print xmin&lt;br /&gt;print fmin&lt;br /&gt;print n&lt;br /&gt;print "-----"&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;(x1,x2,n) = optMinGold(f, -2.0, 2.0)&lt;br /&gt;print x1&lt;br /&gt;print f(x1)&lt;br /&gt;print x2&lt;br /&gt;print f(x2)&lt;br /&gt;print n&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 15 Jun 2008 15:43:19 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5650</guid>
      <author>lrei (Luis Rei)</author>
    </item>
    <item>
      <title>Ruby IRB helper method to pretty-print object methods</title>
      <link>http://snippets.dzone.com/posts/show/2916</link>
      <description>A simple method to print out the methods for an object at run time in a nicely formatted and colorized way. Really useful when using irb&lt;br /&gt;&lt;br /&gt;Place in your .irbrc for easy usage, example here:&lt;br /&gt;http://dotfiles.org/~sd/.irbrc&lt;br /&gt;&lt;br /&gt;usage:&lt;br /&gt;pm object&lt;br /&gt;pm object, :more - shows all methods including base Object methods&lt;br /&gt;pm object, :more, /to/ - shows all methods filtered by regexp&lt;br /&gt;&lt;br /&gt;Coded by sebastian delmont&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    ANSI_BOLD       = "\033[1m"&lt;br /&gt;    ANSI_RESET      = "\033[0m"&lt;br /&gt;    ANSI_LGRAY    = "\033[0;37m"&lt;br /&gt;    ANSI_GRAY     = "\033[1;30m"&lt;br /&gt;&lt;br /&gt;    def pm(obj, *options) # Print methods&lt;br /&gt;      methods = obj.methods&lt;br /&gt;      methods -= Object.methods unless options.include? :more&lt;br /&gt;      filter = options.select {|opt| opt.kind_of? Regexp}.first&lt;br /&gt;      methods = methods.select {|name| name =~ filter} if filter&lt;br /&gt;&lt;br /&gt;      data = methods.sort.collect do |name|&lt;br /&gt;        method = obj.method(name)&lt;br /&gt;        if method.arity == 0&lt;br /&gt;          args = "()"&lt;br /&gt;        elsif method.arity &gt; 0&lt;br /&gt;          n = method.arity&lt;br /&gt;          args = "(#{(1..n).collect {|i| "arg#{i}"}.join(", ")})"&lt;br /&gt;        elsif method.arity &lt; 0&lt;br /&gt;          n = -method.arity&lt;br /&gt;          args = "(#{(1..n).collect {|i| "arg#{i}"}.join(", ")}, ...)"&lt;br /&gt;        end&lt;br /&gt;        klass = $1 if method.inspect =~ /Method: (.*?)#/&lt;br /&gt;        [name, args, klass]&lt;br /&gt;      end&lt;br /&gt;      max_name = data.collect {|item| item[0].size}.max&lt;br /&gt;      max_args = data.collect {|item| item[1].size}.max&lt;br /&gt;      data.each do |item| &lt;br /&gt;        print " #{ANSI_BOLD}#{item[0].rjust(max_name)}#{ANSI_RESET}"&lt;br /&gt;        print "#{ANSI_GRAY}#{item[1].ljust(max_args)}#{ANSI_RESET}"&lt;br /&gt;        print "   #{ANSI_LGRAY}#{item[2]}#{ANSI_RESET}\n"&lt;br /&gt;      end&lt;br /&gt;      data.size&lt;br /&gt;    end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 27 Oct 2006 23:08:01 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2916</guid>
      <author>conorh (Conor Hunt)</author>
    </item>
    <item>
      <title>Get the name of the current method in Ruby</title>
      <link>http://snippets.dzone.com/posts/show/2366</link>
      <description>Found at &lt;a href="http://nubyonrails.com/articles/2006/08/04/seattle-rbbq"&gt;http://nubyonrails.com/articles/2006/08/04/seattle-rbbq&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def method_name&lt;br /&gt;  if  /`(.*)'/.match(caller.first)&lt;br /&gt;    return $1&lt;br /&gt;  end&lt;br /&gt;  nil&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def blah&lt;br /&gt;  puts method_name&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;blah  # =&gt; 'blah'&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 04 Aug 2006 19:22:00 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2366</guid>
      <author>jswizard (JavaScript Wizard)</author>
    </item>
    <item>
      <title>Using Ruby hashes as keyword arguments, with easy defaults</title>
      <link>http://snippets.dzone.com/posts/show/2329</link>
      <description>Similar to many Rails helpers/methods, a lot of the methods I write often use an optional hash of options, or sometimes just a hash only, to simulate keyword arguments (often using symbols).&lt;br /&gt;&lt;br /&gt;The only downside to doing this is you lose out on easily setting default values using Ruby's default method argument values. You might use code something similar to the following to make up for this:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def some_method(opts={})&lt;br /&gt;  my_foo =  opts[:foo] || 'mydefaultfoo'&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;However, as you have more and more keyword options, setting defaults in this way gets rather tedious. Fortunately, Ruby's Hash#merge comes to our rescue (almost) - it allows you to merge the contents of one hash with another. The only problem - any duplicate keys in the hash you are merging will overwrite your original hash values - when it comes to setting default values, we want this to work the other way around; we only want values in the defaults hash to be merged if they do not exist in the original hash. Again, Ruby comes to our rescue - Hash#merge takes a block as an argument and will pass any duplicate values that crop up into the block - we can use this block to decide which value to keep.&lt;br /&gt;&lt;br /&gt;Using the simple monkey patch to the Hash class below, you will no longer have to set each default individually:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Hash&lt;br /&gt;  def with_defaults(defaults)&lt;br /&gt;    self.merge(defaults) { |key, old, new| old.nil? ? new : old } &lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def with_defaults!(defaults)&lt;br /&gt;    self.merge!(defaults) { |key, old, new| old.nil? ? new : old }&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Of course, sticking with Ruby naming conventions, with_defaults() will return a new hash whilst with_defaults!() will change the original hash directly. &lt;br /&gt;&lt;br /&gt;See http://www.lukeredpath.co.uk/index.php/2006/07/27/using-ruby-hashes-as-keyword-arguments-with-easy-defaults/ for further discussion and alternatives.</description>
      <pubDate>Thu, 27 Jul 2006 13:17:06 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2329</guid>
      <author>lukeredpath (Luke Redpath)</author>
    </item>
    <item>
      <title>Temporarily overriding class methods in Ruby</title>
      <link>http://snippets.dzone.com/posts/show/1872</link>
      <description>By Tobi / xal from IRC, and seemingly related to &lt;a href="http://project.ioni.st/post/692#post-692"&gt;this.&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;class Object&lt;br /&gt;  def mock_methods(mock_methods)&lt;br /&gt; &lt;br /&gt;    original = self&lt;br /&gt; &lt;br /&gt;    klass = Class.new(self) do&lt;br /&gt; &lt;br /&gt;      instance_eval do       &lt;br /&gt;        mock_methods.each do |method, proc| &lt;br /&gt;          define_method("mocked_#{method}", &amp;proc)&lt;br /&gt;          alias_method method, "mocked_#{method}"&lt;br /&gt;        end            &lt;br /&gt;      end&lt;br /&gt; &lt;br /&gt;    end&lt;br /&gt; &lt;br /&gt;    begin&lt;br /&gt;      Object.send(:remove_const, self.name.to_s)&lt;br /&gt;      Object.const_set(self.name.intern, klass)&lt;br /&gt; &lt;br /&gt;      yield&lt;br /&gt; &lt;br /&gt;    ensure&lt;br /&gt;      Object.send(:remove_const, self.name.to_s)&lt;br /&gt;      Object.const_set(self.name.intern, original)&lt;br /&gt;    end&lt;br /&gt; &lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt; &lt;br /&gt;class Duck&lt;br /&gt;  def quak; puts "Quak";  end&lt;br /&gt;end&lt;br /&gt; &lt;br /&gt;Duck.new.quak #=&gt; "Quak"&lt;br /&gt; &lt;br /&gt;Duck.mock_methods(:quak =&gt; Proc.new { puts 'Wuff' }) do  &lt;br /&gt;  Duck.new.quak #=&gt; "Wuff"&lt;br /&gt;end&lt;/code&gt;</description>
      <pubDate>Sun, 09 Apr 2006 23:42:56 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1872</guid>
      <author>peter (Peter Cooperx)</author>
    </item>
    <item>
      <title>Example of Detecting Subclasses</title>
      <link>http://snippets.dzone.com/posts/show/158</link>
      <description>class A&lt;br /&gt;  def A.inherited(clazz)&lt;br /&gt;    puts "Hey, #{clazz} is subclassing me"&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt; &lt;br /&gt;class B &lt; A; end&lt;br /&gt; &lt;br /&gt; &lt;br /&gt;Produces:&lt;br /&gt; &lt;br /&gt;$ ruby x.rb&lt;br /&gt;Hey, B is subclassing me</description>
      <pubDate>Wed, 13 Apr 2005 11:03:57 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/158</guid>
      <author>jimweirich (Jim Weirich)</author>
    </item>
  </channel>
</rss>
