Tag Archive for ruby

Return Array of Overridden Methods in Ruby Subclasses

# thanks to the answer here:
# <a href="http://stackoverflow.com/questions/3488203/how-do-i-see-where-in-the-class-hierarchy-a-method-was-defined-and-overridden-in" >http://stackoverflow.com/questions/3488203/how-do-i-see-where-in-the-class-hierarchy-a-method-was-defined-and-overridden-in</a>
class Object
  def self.overridden_methods(parent_class = Object, within_tree = true)
    if within_tree
      defined_methods = ancestors[0..ancestors.index(parent_class) - 1].map { |object| object.instance_methods(false) }.flatten.uniq
      parent_methods = superclass.instance_methods
    else
      defined_methods = instance_methods(false)
      parent_methods = parent_class.instance_methods
    end
    defined_methods & parent_methods
  end
  
  def overridden_methods(parent_class = Object, within_tree = true)
    self.class.overridden_methods(parent_class, within_tree)
  end
  
  def self.overrides?(method, parent_class = Object, within_tree = true)
    overridden_methods(parent_class, within_tree).include?(method.to_s)
  end
  
  def overrides?(method, parent_class = Object, within_tree = true)
    self.class.overrides?(method, parent_class, within_tree)
  end
end

class ParentModel < Object
  def save; end
end

class ChildModel < ParentModel
  def save; end
  def create; end
  def to_s; end
end

class AnotherChildModel < ParentModel
  def save; end
end

class GrandChildModel < ChildModel
  def create; end
end

# what have you overridden from the base `Object` class
p ParentModel.overridden_methods(Object)
#=> []
p ParentModel.overridden_methods(Object, false)
#=> []
p ChildModel.overridden_methods(Object)
#=> ["to_s", "save"]
p ChildModel.overridden_methods(Object, false)
#=> ["to_s"]
p ChildModel.overridden_methods(ParentModel)
#=> ["to_s", "save"]
p ChildModel.overridden_methods(ParentModel, false)
#=> ["to_s", "save"]
p AnotherChildModel.overridden_methods(Object)
#=> ["save"]
p AnotherChildModel.overridden_methods(Object, false)
#=> []
p AnotherChildModel.overridden_methods(ParentModel)
#=> ["save"]
p AnotherChildModel.overridden_methods(ParentModel, false)
#=> ["save"]
p GrandChildModel.overridden_methods(Object)
#=> ["create", "to_s", "save"]
p GrandChildModel.overridden_methods(Object, false)
#=> []
p GrandChildModel.overridden_methods(ParentModel)
#=> ["create", "to_s", "save"]
p GrandChildModel.overridden_methods(ParentModel, false)
#=> []
p GrandChildModel.overridden_methods(ChildModel)
#=> ["create"]
p GrandChildModel.overridden_methods(ChildModel, false)
#=> ["create"]

# note, this probably won't work with modules overriding methods, it just needs to be tweaked a little more

source

adium_status

#!/usr/bin/env ruby

require "rubygems"
require "appscript"
include Appscript

adium = app("Adium")
status_message = "Fiddling with appscript."

adium.accounts.get.each do |a|
  a.go_away(:with_message => status_message)
end

source

Ruby caller experiments

class Kung
  mstr = %-
        def foo
          puts 'Hello World from Kung.foo'
          puts caller(0).inspect
        end
      -
  module_eval mstr
end
    
Kung.new.foo



class Monkey
  line, mstr = __LINE__, %-
        def see
          puts 'Hello World from Monkey.see'
          puts caller(0)
        end
      -
  module_eval mstr, __FILE__, line
end
    
Monkey.new.see


class Monkey
  module_eval(<<-EOS, __FILE__, __LINE__)
        def see
          puts 'Hello World from Monkey.see'
          puts  caller(0)
        end
      EOS
end
    
Monkey.new.see

source

A Test Snippet

  def available?
    status == 1
  end

source

cloby.rb

require 'clojure'
 
class MyClojureObj < Clojure::Object
  def initialize
    dosync { @foo = 'foo' }
  end
 
  attr_accessor :foo
end
 
obj = MyClojureObj.new
puts "obj.foo = " + obj.foo
 
begin
  puts "Setting obj.foo to 'bar'"
  obj.foo = 'bar'
rescue ConcurrencyError
  puts "Oops, need a transaction"
end
 
puts "Trying again with a transaction"
dosync { obj.foo = 'bar' }
puts "Success"
 
puts "obj.foo = " + obj.foo

source

Howto set up a Rake task to run Cucumber features and generate reports

require 'cucumber'
require 'cucumber/rake/task'

desc 'Run Cucumber features and generate an HTML summary, JUnit XML and a plain text log'

Cucumber::Rake::Task.new(:features) do |t|

  t.cucumber_opts = [
                     "--no-color",
                     "--format progress -o log/features.log",
                     "--format junit    -o log/",
                     "--format html     -o log/features.html",
                     "--format pretty"]
end

source

Log4r: how to use RollingFileOutputter: automatically cycle stale logfiles

# How to use RollingFileOutputter 

$: << "../src"

require 'log4r'

include Log4r 

puts "this will take a while" 

# example of log file being split by time constraint 'maxtime'

config = {

  "filename" => "logs/TestTime.log",

  "maxtime" => 10,

  "trunc" => true

}

timeLog = Logger.new 'WbExplorer'

timeLog.outputters = RollingFileOutputter.new("WbExplorer", config)

timeLog.level = DEBUG 

# log something once a second for 100 seconds

100.times { |t|

  timeLog.info "blah #{t}"

  sleep(1.0)

} 

# example of log file being split by space constraint 'maxsize'

config = {

  "filename" => "logs/TestSize.log",

  "maxsize" => 16000,

  "trunc" => true

}

sizeLog = Logger.new 'WbExplorer'

sizeLog.outputters = RollingFileOutputter.new("WbExplorer", config)

sizeLog.level = DEBUG 

# log a large number of times

100000.times { |t|

  sizeLog.info "blah #{t}"

} 

puts "done! check the two sets of log files in logs/ (TestTime and TestSize)"

source

ruby and ruby ee

Ruby and Ruby EE in same system

 ln -s /opt/ruby/bin/ruby /usr/bin/ruby_ee
 ln -s /opt/ruby/bin/gem /ust/bin/gem_ee



gem_install.sh

sudo gem install $1
sudo gem_ee install $1

source

ActionMailer Gmail Example

require "action_mailer"
 
class Notifier < ActionMailer::Base
  def email(address)
    recipients  "<a href="mailto:recipient@mail.com">recipient@mail.com</a>"
    from        "<a href="mailto:you@yourdomain.com">you@yourdomain.com</a>"
    subject     "Hello World"
    body        address
  end
end
 
# TLS settings are for gmail, not needed for other mail hosts.
Notifier.delivery_method = :smtp
Notifier.smtp_settings = {
  :tls => true,
  :enable_starttls_auto => true,
  :address => "smtp.gmail.com",
  :port => 587,
  :domain => "yourdomain.com",
  :user_name => "<a href="mailto:you@yourdomain.com">you@yourdomain.com</a>",
  :password => "12345",
  :authentication => :plain
}
 
Notifier.deliver_email("<a href="mailto:recipient@mail.com">recipient@mail.com</a>")

source

RoR – Select Box Using Existing Model

# Replace |model| with whatever you want to use. ie. |whatever|
<%= select(:object, :name, Model.all.collect {|model| [model.name, model.id]}) %>

source