Go's concurrency model on top of JRuby
Channel.new
creates a channel, and you can optionally pass it a capacity: Channel.new(200)
Use <<
to send on a channel: chan << "stuff"
def sqrt(i: Int >= 0): Int >= 0 { | |
... | |
} | |
def good_input() { | |
i = Int.parse(stdio.read_line) | |
if i >= 0 { | |
print "sqrt: #{sqrt(i)} | |
} else { | |
print "Cannot take the sqrt of a negative number" |
Most Ruby code makes heavy use of mutable state, which often contributes to long term maintenance problems. Mutability can lead to code that's difficult to understand, libraries and applications that aren't thread-safe, and tests that are slow and brittle. Immutability, on the other hand, can make code easy to reason about, safe to use in multi-threaded environments, and simple to test. Doesn't that sound nice?
This talk answers the question "why immutability?", covers the building blocks of immutable code, such as value objects and persistent data structures, as well as higher order concepts that make heavy use of immutability, such as event sourcing and pure functions, and finally discusses the tradeoffs involved in going immutable.
Michael Fairley is a developer at Braintree Payments, where he uses Ruby to make it easy for businesses around the world (including the UK!) to accept credit card payments online. He’s an active open source contributor, and maintains a han
the `path_param`/`auth` stuff seems ugly :-/ | |
How to convert response to json, etc. (ActiveModel Serializers?) | |
Providers? | |
Routing/registry |
def memoize(&blk) | |
cache = {} | |
lambda do |*args| | |
return cache[args] if cache.has_key?(args) | |
cache[args] = blk.call(*args) | |
end | |
end | |
define_singleton_method(:fib, &memoize{ |n| | |
if n < 2 |
source :rubygems | |
gem 'activerecord', :require => 'active_record' |
require 'benchmark' | |
require 'draper' | |
class Model | |
# 300 is about the fewest methods an ActiveRecord model will have | |
300.times do |i| | |
define_method :"method_#{i}" do | |
i | |
end | |
end |
> "@RubyTips".methods.sort | |
=> [:!, :!=, :!~, :%, :*, :+, :<, :<<, :<=, :<=>, :==, :===, :=~, :>, :>=, :[], :[]=, :__id__, :__send__, :ascii_only?, :between?, :bytes, :bytesize, :capitalize, :capitalize!, :casecmp, :center, :chars, :chomp, :chomp!, :chop, :chop!, :chr, :class, :clear, :clone, :codepoints, :concat, :count, :crypt, :define_singleton_method, :delete, :delete!, :display, :downcase, :downcase!, :dump, :dup, :each_byte, :each_char, :each_codepoint, :each_line, :empty?, :encode, :encode!, :encoding, :end_with?, :enum_for, :eql?, :equal?, :extend, :force_encoding, :freeze, :frozen?, :getbyte, :gsub, :gsub!, :hash, :hex, :include?, :index, :initialize_clone, :initialize_dup, :insert, :inspect, :instance_eval, :instance_exec, :instance_of?, :instance_variable_defined?, :instance_variable_get, :instance_variable_set, :instance_variables, :intern, :is_a?, :kind_of?, :length, :lines, :ljust, :lstrip, :lstrip!, :match, :method, :methods, :next, :next!, :nil?, :object_id, :oct, :ord, :partition, :private_m |
ruby-1.8.7-p330 :001 > 1 + 1 | |
=> 2 | |
ruby-1.8.7-p330 :002 > a = _ | |
=> 2 | |
ruby-1.8.7-p330 :003 > a | |
=> 2 |
a = nil || 1 | |
puts a # prints 1 | |
b = nil or 1 | |
puts b # prints nil | |
c = 1 && 2 | |
puts c # prints 2 |