Skip to content

Instantly share code, notes, and snippets.

@burke
Created March 22, 2010 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save burke/340049 to your computer and use it in GitHub Desktop.
Save burke/340049 to your computer and use it in GitHub Desktop.
# This isn't necessarily "better", or even "useful" in any way, just sort of fun.
class Object
# I like how I can actually create methods with these names.
def if
@__if_result = yield self
self
end
def then
@__if_result ? yield self : self
end
def else
@__if_result ? self : yield self
end
end
# code sample from elsewhere.
# http://github.com/nateware/redis-objects/blob/master/ATOMICITY.rdoc
@course = Course.find(1)
if @course.num_students < 30
@course.course_students.create!(:student_id => 101)
@course.num_students += 1
@course.save!
else
# course is full
end
# revised version.
Course.find(1).if { |course|
course.num_students < 30
}.then { |course|
course.course_students.create!(:student_id => 101)
course.num_students += 1
}.else { |course|
# Do nothing!
}.save!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment