background preloader

Ruby

Facebook Twitter

Ruby core classes aren't thread safe | Jesse Storimer's Blog. A few days I shared a quote on Twitter: > Ruby Arrays 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. Does this mean I shouldn't use Arrays? 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) inventory.decrease(:pants) Now let me ask you this: is this thread-safe? Let's exercise this code to find out. 4000.times do puts inventory[:hats] Now let's exercise the code again, with threads this time. RubySource » For Ruby & Rails Developers | Ruby on Rails Tutorials | Learn Ruby | Gems | Strings and MoreRubySource. 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. 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. 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. I've added paragraphs like this where necessary. 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. Ruby-Doc.org: Documenting the Ruby Language. Must-Read Ruby Links.

Every programming language has a number of works you just cannot not read. 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. The Ruby Homepage - This might seem like a no-brainer, the Ruby homepage. Stack Overflow - In the same vein as the Ruby mailing lists is Stack Overflow. Ruby-Doc.org - Yes, you probably have Ruby documentation on your system already. Ruby Inside - Peter Cooper has been slavishly updating this blog for years now. Ruby Flow - Another one from Peter Cooper, but this time anyone can submit. Don’' Know Metaprogramming In Ruby? Don’t 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 One of the most impressive aspects of Ruby is its metaprogramming capabilities. As a dynamic language, Ruby gives you the freedom to define methods and even classes during runtime. Metaprogramming with Ruby, one can do in a few minutes what other languages may take hours to do. Ruby Classes and class inheritanceInstance methods and instance variablesRaising exceptions and standard Ruby notations: MyClass#method_name to show that method_name is a method in MyClass # => to show the return value of a method What is Metaprogramming? According to Wikipedia: Adding methods in the context of an object In Ruby, everything is an object.

To see where this feature of Ruby might be useful, let’s look at a more common example: writing class methods. 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. 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. What is “metaprogramming”? Metaprogramming is best explained as programming of programming. Metaprogramming can be used a way to add, edit or modify the code of your program while it’s running. Understanding how Ruby calls methods Before you can understand the full scope of metaprogramming, it’s imperative that you grasp how Ruby finds a method when it is called. Things get more complicated however when the object is an instance of a class which has inherited from another class: It’s a slightly different story when we call the babe.eats() method, though. Introducing the Singleton class. Ruby Metaprogramming: Part II » RubySource. Welcome back to Ruby Metaprogramming! 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. Let’s get started with a nice, simple example: 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.