Ruby Stdlib
@RubyStdlib
One tweet a day about Ruby's Core and Standard Library. Account managed by @plexus
You might like
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
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
@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"
@RubyStdlib @mpapis I've got a couple mwdesilva.com/posts/90-simpl… mwdesilva.com/posts/88-monsi… gist.github.com/bsodmike/69937…
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!
United States Trends
- 1. INCOGNITO 4,240 posts
- 2. Cynthia 86.1K posts
- 3. #WorldKindnessDay 13.2K posts
- 4. CarPlay 2,182 posts
- 5. Katie Couric 3,716 posts
- 6. #NXXT_EarningReport N/A
- 7. Massie 88.3K posts
- 8. #LoveDesignEP7 124K posts
- 9. RIN AOKBAB BEGIN AGAIN 125K posts
- 10. Encyclopedia Galactica 5,015 posts
- 11. Bonhoeffer 2,253 posts
- 12. GRABFOOD LOVES LINGORM 994K posts
- 13. Larry Brooks 2,862 posts
- 14. Tommy James N/A
- 15. #thursdayvibes 3,983 posts
- 16. Black Mirror 3,256 posts
- 17. Bongino 8,855 posts
- 18. $NXXT 1,509 posts
- 19. Michael Burry 12.1K posts
- 20. #BuyTheDip N/A
You might like
Something went wrong.
Something went wrong.