Notes posted by dennismonsewicz

RSS feed
January 8, 2013
1 thank

Helper Methods inside of a Controller

When needing to include a helper method inside of a controller, instead of including the ENTIRE helper module, simply use this method like so:

module ApplicationHelper
  def fancy_helper(str)
    str.titleize
  end
end

class MyController < ApplicationController
   def index
     @title = view_context.fancy_helper "dogs are awesome"
   end
end
June 11, 2012
0 thanks

more_than? instance method

Over the weekend I kept running into instances where I was writing code like this:

Code example

arr = ['hello', 'world']

if arr.length > 2
 # do stuff
else
 # do something else
end

So I ended up extending the core and adding an instance method of more_than?

Code example

class Array
  def more_than?(num)
    length > num
  end
end

Usage

arr = ['hello', 'world']
puts "Hello" if arr.more_than? 1