Skip to content

Instantly share code, notes, and snippets.

View orend's full-sized avatar

Oren Dobzinski orend

View GitHub Profile
@orend
orend / gist:3797691
Created September 28, 2012 02:55
array join shortcut
[1, 2, 3] * ' '
@orend
orend / gist:3872362
Created October 11, 2012 13:41
tricks from jeg2 talk
p (1..10).inject(:*) # instead of: inject(&:*)
# case on ranges
age = rand(1..100)
p age
case age
when -Float::INFINITY..20
puts "You're too young."
when 21..64
type Word = String
type Sentence = List[Word]
type Occurrences = List[(Char, Int)]
val dictionary: List[Word] = loadDictionary
def wordOccurrences(w: Word): Occurrences = w.toLowerCase().toList.groupBy(x => x).mapValues(x=>x.length).toList.sorted
/** Converts a sentence into its character occurrence list. */
def sentenceOccurrences(s: Sentence): Occurrences = wordOccurrences((s foldLeft "") (_ + _))
odobzinski:~/lib/pelusa $ /Users/odobzinski/.rbenv/versions/rbx-2.0.0-rc1/1.9/bin/pelusa ~/dev_exclusions/assistly/lib/es_search/agent_builder.rb
ϟ Pelusa ϟ
----------
/Users/odobzinski/dev_exclusions/assistly/lib/es_search/agent_builder.rb
class TokenItem
✿ Is below 50 lines ✓
✿ Uses less than 3 ivars ✗
This class uses 3 instance variables: @token, @type, @value.
✿ Respects Demeter law ✗
@orend
orend / Fuctional FizzBuzz in Ruby
Last active October 13, 2015 10:17
FizBuzz curry
f = proc{|string, num, x| x % num == 0 ? string : x}.curry
f3 = f.('Fizz',3)
f15 = f.('FizzBuzz',15)
f5 = f.('Buzz',5)
puts (1..100).map(&f15).map(&f5).map(&f3)
# and here's another approach
module Enumerable
# map all elements for which the condition is true, leave the rest intact
@orend
orend / gist:4645790
Last active December 11, 2015 18:59 — forked from brianstorti/gist:3839690

#Practical Object Oriented Design in Ruby

Design that anticipate specific future requirements almost always end badly.
Practical design does not anticipate what will happen to your application, it merely accepts that something
will and that, in the present, you cannot know what. It does not guess the future; it preserves your options for accommodating the future.
It doesn't choose; it leaves you room to move.
The purpose of design it to allow you to do it later and its primary goal is to reduce the cost of change.

Design is more the art of preserving changeability than it is the act of achieving perfection.

@orend
orend / gist:5134300
Created March 11, 2013 13:40
Streaming with csv, from ruby tapas
require 'csv'
def memstats
size = `ps -o size= #{$$}`.strip.to_i
end
memstats #4900
CSV.open('visitors.csv', headers: true) do |csv|
visitors = csv.each # Enumerator
memstats # 5164
@orend
orend / gist:5196398
Last active December 15, 2015 03:39
default values to hashes (from ruby tapas)
# a default value of array allows us to implicitly initialize accessed fields
h = Hash.new{ |h ,k| h[k] = []}
h['a'] << "c"
h['a'] << "d"
>> h
{
"a" => [
[0] "c",
[1] "d"
@orend
orend / gist:7084314
Last active December 26, 2015 03:09
before:
<%= if profile.has_experience? && profile.experience_public? %>
<p><strong>Experience:</strong> <%= user_profile.experience %></p>
<% end %>
//after
<h2><%= user_profile.full_name %></h2>
<%= user_profile.bio_html %>
@orend
orend / gist:9099893
Last active August 29, 2015 13:56
Time travel and dependencies
def age(with_respect_to = Time.now)
...
end