RubyStdlib's profile picture. One tweet a day about Ruby's Core and Standard Library. Account managed by @plexus

Ruby Stdlib

@RubyStdlib

One tweet a day about Ruby's Core and Standard Library. Account managed by @plexus

If you messed with the code of an installed gem, you can make sure it's clean again with `gem pristine gem_name'


The collect/inject/select/detect methods are taken from Smalltalk. The map/reduce/filter/find aliases are from Lisp. What's your style?


Ruby has no convention for "updating" an immutable object. Which of these 4 options do you prefer? gist.github.com/plexus/42c6c9c…


"John von Neumann".split(" ") # => ["John", "von", "Neumann"] "John von Neumann".split(" ", 2) # => ["John", "von Neumann"]


Use public_instance_methods(false) to ignore methods inherited from the superclass. Use respond_to?(method, true) to include private methods


Homework: ask @chastell why on earth you would want to use WeakRef


Extended regular expressions and named captures go really well together. Check it out! gist.github.com/9a9ee2e00a4f80…


When storing a boolean in a variable, ask yourself, "Could I use a flip-flop operator instead?" #actuallydont


Stick 'to_enum(__method__) unless block_given?' at the beginning of any method that takes a block to get Enumerator goodness.


Careful though, Ruby will reuse the object h=Hash.new([]) h[:a] << :x h[:b] << :y h[:c] #=> [:x,:y] In this case: Hash.new{|h,k| h[k]=[]}


It's possible to give a Hash a default upon construction, or afterwards h1=Hash.new(:a) h2={} h2.default=:b h1[:x] #=> :a


def curry_method(name) method(name).to_proc.curry end alias λ curry_method alias haskell curry_method alias schönefinkel curry_method


Working with binary files or packets? Check String#unpack and Array#pack. They pack a lot of punch bit.ly/1ePfdui


Turn an Enumerable into an infinite Enumerator def cycle(a) ; Enumerator.new{|y|loop{a.each{|f|y<<f}}} ; end cycle([:a, :b, :c]).take(10)


require "English" (mind the capital) for less cryptic versions of $< $> $/ $$ etc. Useless for code golf! ruby-doc.org/stdlib-2.0.0/l… /by @pxlpnk


Ruby Stdlib reposted

@RubyStdlib @mr_foto =~ looks like a lizzard/dragon !~ like a water-tap


lambdas are almost like procs/blocks, but they check arity, and "return" only exits the lambda, the block equivalent would be "next"


Use 'if __FILE__ == $0' to check if the file is being executed as a script. Great for including tests in the same file! /by @pxlpnk


Let's try something new, guest posts! What's your favorite class/method/trick in Ruby core/stdlib? Link to a gist if you want to show off!


This account does not follow anyone
Loading...

Something went wrong.


Something went wrong.