This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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] ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Which version is more readable? | |
[Flags] | |
public enum HttpVerbs { | |
Get = 1 << 0, | |
Post = 1 << 1, | |
Put = 1 << 2, | |
Delete = 1 << 3, | |
Head = 1 << 4 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#light | |
open System | |
type Game()= | |
member this.NewScore(winnersScore, othersScore)= | |
match winnersScore with | |
|"0" -> "15", othersScore | |
|"15" -> "30", othersScore | |
|"30" -> "40", othersScore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Weather | |
attr_accessor :temperature, :humidity | |
def initialize another_weather | |
self.temperature = another_weather.temperature | |
self.humidity = another_weather.humidity | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module MyModule | |
def self.included base | |
included_classes << base unless included_classes.include?(base) | |
end | |
def self.included_classes | |
@@included ||= [] | |
end | |
end |
OlderNewer