background preloader

Ruby

Facebook Twitter

Ruby core classes aren't thread safe. A few days I shared a quote on Twitter: > Ruby Arrays aren't thread-safe, [...]

Ruby core classes aren't thread safe

MRI's threading implementation accidentally makes them thread-safe. It saw a bunch of retweets and discussion, but I daresay that the nuances of that statement were lost on a lot of developers. Heck, a few months ago, I wouldn't have really known what that meant. It sounds like something bad. That's a big question, and I think the answer lies in education. Let's dig in with an example. Here's a very simple inventory tracking class: class Inventory def initialize(stock_levels) @stock = stock_levels end def decrease(item) @stock[item] -= 1 def [](item) @stock[item] It's trivially simple.

Inventory = Inventory.new(:tshirt => 200, :pants => 340, :hats => 4000) inventory.decrease(:hats) RubySource » For Ruby & Rails Developers. Ruby Tutorials Forum. Ruby Forum. Rake - Ruby Tutorials. The Ruby Programming Language. Four Guidelines That I Feel Have Improved My Code. I have been thinking a lot about isolation, dependencies and clean code of late.

Four Guidelines That I Feel Have Improved My Code

I know there is a lot of disagreement with people vehemently standing in both camps. I certainly will not say either side is right or wrong, but what follows is what I feel has improved my code. I post it here to formalize some recent thoughts and, if I am lucky, get some good feedback. Before I rush into the gory details, I feel I should mention that I went down this path, not as an architecture astronout, but out of genuine pain in what I was working on. My models were growing large. I started watching Gary Bernhardt’s Destroy All Software screencasts. On top of DAS, I started reading everything I could on the subject of growing software, clean code and refactoring. I was literally prowling about like a lion, looking for the next book I could devour. Over the past few months as I have tried to write better code, I have definitely learned a lot. Guideline #1. The problem is context.

Create More Classes. Ruby Hacking Guide. The Ruby Toolbox - Know Your Options! HTTP clients in The Ruby Toolbox. Ruby HTTP clients features. 21 Ruby Tricks You Should Be Using In Your Own Code. Writing for Ruby Inside, I get to see a lot of Ruby code.

21 Ruby Tricks You Should Be Using In Your Own Code

Most is good, but sometimes we forget some of Ruby's shortcuts and tricks and reinvent the wheel instead. In this post I present 21 different Ruby "tricks," from those that most experienced developers use every day to the more obscure. Whatever your level, a refresh may help you the next time you encounter certain coding scenarios. Note to beginners: If you're still learning Ruby, check out my Beginning Ruby book. 2009 Update: This post was written in early 2008 and looking back on it, there are a couple of tricks that I wouldn't recommend anymore - or to which extra warnings need to be added. 1 - Extract regular expression matches quickly A typical way to extract data from text using a regular expression is to use the match method.

Email = "Fred Bloggs <fred@bloggs.com>"email.match(/<(.*?) Ultimately, using the String#[] approach is cleaner though it might seem more "magic" to you. X = 'this is a test' x[/[aeiou].+? Puts x == 10 ? Ruby-Doc.org: Documenting the Ruby Language. Must-Read Ruby Links. Every programming language has a number of works you just cannot not read.

Must-Read Ruby Links

Perl has the camel book, C++ has "The C++ Programming Lanugage," etc. Ruby does have it's own dead tree you cannot not read, "Programming Ruby: The Pragmatic Programmer's Guide," the so-called "pickaxe book," but that's covered so many places and recommended by everyone. Here are some links you must read in order to understand Ruby. Ruby Koans - A "koan" is a story or dialog crafted to make a student think. The story itself doesn't teach anything, the story forces you to investigate things yourself. Don’' Know Metaprogramming In Ruby? Don’t Know Metaprogramming In Ruby?

Don’' Know Metaprogramming In Ruby?

This guest post is by Gavin Morrice, who is a freelance Ruby/Rails developer based in Edinburgh. He likes sharing Rails tips on his site. When he’s not writing code he’s usually at the gym, reading or cooking. Introduction. Ruby Metaprogramming: Part I. If you’re working with Ruby, chances are by now you’ve heard the word “metaprogramming” thrown around quite a lot.

Ruby Metaprogramming: Part I

You may have even used metaprogramming, but not fully understood the true power or usefulness of what it can do. By the end of this article, you should have a firm grasp not only of what it is, but also what it capable of, and how you can harness one of Ruby’s “killer features” in your projects. Ruby Metaprogramming: Part II » RubySource. Welcome back to Ruby Metaprogramming!

Ruby Metaprogramming: Part II » RubySource

In part one we looked at what Metaprogramming is and how it works; we explored deep into the internals of Ruby’s method lookup system and walked through how creating Singleton Classes fits into that mechanism. Now for the good part: applying it all. Mocking objects for testing Some of the most useful features of Ruby’s metaprogramming have been shown off countless times in the vast array of testing frameworks available. Whilst a lot of these frameworks use various forms of metaprogramming to create their APIs, we’re going to explore one of the most important uses of metaprogramming used in Ruby test frameworks: metaprogramming for mocking and stubbing. You can probably start to draw conclusions about how this might work given what you now know about metaprogramming.

We’re going to pretend we’re managing a library for a day, and that we need to record what books are borrowed out and by whom. Dynamic methods Let’s start by creating our Country class: