Posts Tagged ‘idioms’

Patterns for require (3)

dir = File.dirname(__FILE__)
require dir + '/core_ext'
require dir + '/randexp/parser'
require dir + '/randexp/reducer'
require dir + '/randexp/dictionary'
require dir + '/randgen'
require dir + '/wordlists/real_name'

Patterns for require (2)

Dir.glob('app/models/*.rb').each { |file| require file }

handling an application’s version in ruby

module VERSION #:nodoc:
  MAJOR = 0
  MINOR = 2
  TINY  = 1

  STRING = [MAJOR, MINOR, TINY].join('.')
end

Patterns for require (1)

dir = File.dirname(__FILE__)
require File.expand_path("#{dir}/test_helper")
require File.expand_path("#{dir}/arithmetic_node_classes")
require File.expand_path("#{dir}/lambda_calculus_node_classes")

alias_method_chain

>> class A
>>   def foo
>>     puts "old foo"
>>   end
>>   def foo_with_feature
>>     puts "new foo"
>>   end
>>   alias_method_chain :foo, :feature
>> end
=> A
>> a = A.new
=> #
>> a.foo
new foo
=> nil
>> a.foo_with_feature
new foo
=> nil
>> a.foo_without_feature
old foo
=> nil

rescue nil with ruby

>> bar = nil
=> nil
>> { :foo => bar ? bar.baz : nil }
=> {:foo=>nil}

# vs.

>> { :foo => (nil.bla rescue nil) }
=> {:foo=>nil}

# does rescue nil win in terms of readability?
# It can be argued, that exception handling
# is not the right way to process this kind logic,
# since it is not always exceptional behaviour
# that motivates the use of rescue nil