# invoke a new session
irb(main):001:0> irb
# list open sessions
irb.1(main):001:0> jobs
#0->irb on main (#<Thread:0x400fb7e4> : stop)
#1->irb#1 on main (#<Thread:0x40125d64> : running)
# change the active session
irb.1(main):002:0> fg 0
ActiveRecord::ConnectionAdapters::Column.value_to_boolean(parameter) |
In researching topics for RailsCasts I often read code in Rails and other gems. This is a great exercise to do. Not only will you pick up some coding tips, but it can help you better understand what makes code readable.
A common practice to organize code in gems is to divide it into modules. When this is done extensively I find it becomes very difficult to read. Before I explain further, a quick detour on instance_eval
.
You can find instance_eval
used in many DSLs: from routes to state machines. Here's an example from Thinking Sphinx.
class Article < ActiveRecord::Base
module AuthHelper | |
def http_login | |
user = 'username' | |
pw = 'password' | |
request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(user,pw) | |
end | |
end | |
module AuthRequestHelper | |
# |
!!! 5 | |
%html | |
%head | |
%title= "Your Website" | |
%meta{ :content => "", :name => "description" } | |
%meta{ :content => "", :name => "author" } | |
%meta{ :content => "3 days", :name => "revisit-after" } | |
%link{ :href => "http://creativecommons.org/licenses/by/3.0/", :rel => "license", :title => "Creative Commons Attribution 3.0 Unported License" } | |
%link{ :href => "/feed", :rel => "alternate", :title => "Atom", :type => "application/atom+xml" } | |
%link{ :href => "/css/screen.css", :media => "screen", :rel => "stylesheet" } |
This gist was writen in 2012 and it was solving specific problem in Rails & SimpleForm. Some fellow developers were pointing out this may be out dated concept. That's why I advise everyone to read comment section bellow to have a full grasp of alternative solutions
other sources that may be helpful to understand why this may not be best idea:
create_table :users, id: false do |t| | |
t.uuid :id, primary: true, null: false | |
# ... | |
end |
<h1> | |
Stop the App Store upgrade prompt</h1> | |
<p> | |
Fortunately I saved my copy of Skitch (version 1.0.12) after hearing <a href="http://twitter.com/danielpunkass">Daniel Jalkut</a> (developer at <a href="http://www.red-sweater.com/">Red Sweater Software</a> best known for <a href="http://www.red-sweater.com/marsedit/">MarsEdit</a> and <a href="http://www.red-sweater.com/fastscripts/">FastScripts</a>) mention that a brief stint with Skitch 2.0 had sent him back to Skitch version 1.</p> | |
<p> | |
The only problem is that the Mac App Store app kept telling me that Skitch needs to be updated. I even went into my Purchases list and '"hid" Skitch, but still the App Store kept telling me that it wanted to update Skitch.</p> | |
<p> | |
I seem to have found a solution, which was to quit Skitch.app, and then locate the app in /Applications/ using the Finder. Once there, I control-clicked on the app and chose "Show Package Contents":</p> | |
<p style="text-align:center"> | |
<img alt="" border="0" height="143" src="htt |
# app/controller/articles_controller.rb | |
around_filter { |controller, action| controller.send(:add_published_scope, Article){ action.call } } | |
# app/controller/application_controller.rb | |
def add_published_scope(klass = nil, &block) | |
unless klass | |
klass = controller_name.classify.constantize | |
end | |
klass.with_scope(:find => klass.where("published_at <= ?", Time.zone.now)) do | |
yield |
my_array = %w[test one two three] | |
# Inject takes the value of the block and passes it along | |
# This often causes un-intended errors | |
my_array.inject({}) do |transformed, word| | |
puts transformed | |
transformed[word] = word.capitalize | |
end | |
# Output: | |
# {} |