Skip to content

Instantly share code, notes, and snippets.

#Instead of the following
Mail::Configuration.instance.retriever_method :pop3, pop3_options_hash
Mail.all.each do |mail|
...
end
#I would like to write one of the following
#1. The following line will instantiate a receiver using the global config
#You have seen something like this, if null replace with a blank array
an_array = (another_array || [] ) + (yet_another_array || [])
#But you could do this as well
an_array = another_array.to_a + yet_another_array.to_a
#Or you have seen this
an_array = another_array + (an_element_or_array.is_a?(Array) ? an_element_or_array : [an_element] )
@smsohan
smsohan / RubyUncommonMethods
Created November 12, 2010 05:48
Some interesting advanced level Ruby coding that you will see in mature code
#the new tap method of ruby will always return itself, but lets you play with!
data = (1..10).to_a
data.tap{|x| print x}.to_a.join(', ')
p
#alias will redirect method calls to method with a different name, useful for api changing
class SomeClass
def new_method(x)
p "The value is #{x}"
@smsohan
smsohan / RubyAdvancedFeatures
Created November 12, 2010 05:50
Some advanced ruby features
#the new tap method of ruby will always return itself, but lets you play with!
data = (1..10).to_a
data.tap{|x| print x}.to_a.join(', ')
p
#alias will redirect method calls to method with a different name, useful for api changing
class SomeClass
def new_method(x)
p "The value is #{x}"
@smsohan
smsohan / C# FlagsAttribute with Enum
Created January 7, 2011 20:52
This excerpt is from ASP.NET MVC - the first example seems to be too geeky. Agree?
Which version is more readable?
[Flags]
public enum HttpVerbs {
Get = 1 << 0,
Post = 1 << 1,
Put = 1 << 2,
Delete = 1 << 3,
Head = 1 << 4
}
@smsohan
smsohan / DrinkRails.scpt
Created January 10, 2011 19:03
An AppleScript code that I use to generate the LI tags with blog title, URL and author from NetNewsWire
-- Build text string
set s to "<li>"
tell application "NetNewsWire"
set s to s & "<a href=\"" & (URL of selectedHeadline) & "\">"
set s to s & (title of selectedHeadline) & "</a>"
set s to s & (creator of selectedHeadline)
end tell
set s to s & "</li>"
-- Set the contents to the ClipBoard
@smsohan
smsohan / fsharp_tennis.fs
Created March 21, 2011 22:23
F# code that simulates a lawn tennis game points
#light
open System
type Game()=
member this.NewScore(winnersScore, othersScore)=
match winnersScore with
|"0" -> "15", othersScore
|"15" -> "30", othersScore
|"30" -> "40", othersScore
@smsohan
smsohan / weather.rb
Created July 26, 2011 17:51
Ruby code of a simple class
class Weather
attr_accessor :temperature, :humidity
def initialize another_weather
self.temperature = another_weather.temperature
self.humidity = another_weather.humidity
end
end
@smsohan
smsohan / should_you_test.rb
Created August 10, 2011 05:21
what_to_test
#example_code
def search(query, page_number)
third_party_search_library.search("*{query}*").from(page_number * page_size).size(page_size).matched_results
end
#example_unit_test
it "should search for partial match and return paged result" do
third_party_search_library.should_receive(:search).with('*new york*').and_return(cities)
cities.should_receive(:from).with(40).and_return(cities)
cities.should_receive(:size).with(20).and_return(cities)
@smsohan
smsohan / module_mixed_in_classes.rb
Created August 12, 2011 15:02
Listing all mixed in classes of a module
module MyModule
def self.included base
included_classes << base unless included_classes.include?(base)
end
def self.included_classes
@@included ||= []
end
end