Functional programming for a Rubyist
Command-line programs
def qsort(list) | |
return list if list.empty? | |
x, *xs = list | |
smaller, larger = xs.partition { |n| n < x } | |
qsort(smaller) + [x] + qsort(larger) | |
end | |
describe 'qsort' do |
require 'parslet' | |
class FooParser < Parslet::Parser | |
rule(:space) { match('\s').repeat(1) } | |
rule(:space?) { space.maybe } | |
rule(:dot) { str('.') >> space? } | |
rule(:colon) { str(':') >> space? } | |
rule(:comma) { str(',') >> space? } | |
rule(:comma?) { comma.maybe } |
Loading development environment (Rails 3.2.19) | |
irb(main):001:0> Time.zone | |
=> #<ActiveSupport::TimeZone:0x007fa2e4533d68 @name="Osaka", @utc_offset=nil, @tzinfo=#<TZInfo::TimezoneProxy: Asia/Tokyo>, @current_period=nil> | |
irb(main):002:0> p = Plan.new | |
=> #<Plan id: nil, decided_on: nil> | |
irb(main):003:0> time = 1.day.ago | |
=> Fri, 26 Sep 2014 06:31:54 JST +09:00 | |
irb(main):004:0> time.to_date | |
### | |
=> Fri, 26 Sep 2014 |
source 'https://rubygems.org' | |
gem 'activerecord', '~>4.0' | |
gem 'sqlite3' |
source 'https://rubygems.org' | |
gem 'activerecord', '~>3.0' | |
gem 'sqlite3' |
source 'https://rubygems.org' | |
gem 'activerecord' | |
gem 'sqlite3' |
params = { some: :thing } | |
oops = {} | |
def kwargs(some:) | |
puts some | |
end | |
def hash(args) | |
puts args.fetch(:some) | |
end |
require 'active_record' | |
class Foo < ActiveRecord::Base | |
establish_connection adapter: 'sqlite3', database: ':memory:' | |
connection.create_table :foos do |t| | |
t.string :name, null: false | |
end | |
#has_many :bars |
# Live cells with < 2 living neighbors dies to underpop | |
# > 3 living neighbors dies to overpop | |
# Otherwise, lives! | |
# | |
# Dead cells with exactly 3 living neighbors is born | |
# . 0 . | |
# . 0 . | |
# . 0 . | |
# |