Archive for the ‘ Ruby ’ Category

High Scalability – High Scalability

High Scalability – High Scalability.

Playfish’s Social Gaming Architecture – 50 Million Monthly Users and Growing

Ten million players a day and over fifty million players a month interact socially with friends using Playfish games on social platforms like The Facebook, MySpace, and the iPhone. Playfish was an early innovator in the fastest growing segment of the game industry: social gaming, which is the love child between casual gaming and social networking. Playfish was also an early adopter of the Amazon cloud, running their system entirely on 100s of cloud servers. Playfish finds itself at the nexus of some hot trends (which may by why EA bought them for $300 million and they think a $1 billion game is possible): building games on social networks, build applications in the cloud, mobile gaming, leveraging data driven design to continuously evolve and improve systems, agile development and deployment, and selling virtual good as a business model.

How can a small company of about 100 people makes this all happen? To explain the magic I interviewed Playfish’s Jodi Moran, Senior Director of Engineering, and Martin Frost, Chief Architext, first Engineer and Operations guy at Playfish. Lots of good stuff, so let’s move on to the nitty gritty.

Rubinius : Use Ruby

via Rubinius : Use Ruby™.

Technically, what is Rubinius?

Rubinius is an implementation of the Ruby programming language.

The Rubinius bytecode virtual machine is written in C++, incorporating LLVM to compile bytecode to machine code at runtime. The bytecode compiler and vast majority of the core classes are written in pure Ruby.

To interact with the rest of the system, the VM provides primitives which can be attached to methods and invoked. Additionally, FFI provides a direct call path to most C functions.

Rubinius uses a precise, compacting, generational garbage collector. It includes a compatible C-API for C extensions written for the standard Ruby interpreter (often referred to as MRI—Matz’s Ruby Implementation).

How compatible is Rubinius?

From the start, compatibility has been critical to us. To that end, we created the RubySpec to ensure that we maintained parity with official Ruby. We are currently at a 93% RubySpec pass rate and growing everyday.

For now Rubinius is targeting MRI 1.8.7 (1.9 is on the post 1.0 list). Most Gems, Rails plugins and C-Extensions work right out of the box. If you find a bug, let us know and we’ll get on top of it.

Ruby String Concatenation and Comparison – Techotopia

Ruby String Concatenation and Comparison – Techotopia.

Concatenating Strings in Ruby

If you’ve read any of the preceding chapters in this book you will have noticed that Ruby typically provides a number of different ways to achieve the same thing. Concatenating strings is certainly no exception to this rule.

Strings can be concatenated using the + method:

myString = "Welcome " + "to " + "Ruby!"
=> "Welcome to Ruby!"

In the interests of brevity, you can even omit the + signs:

myString = "Welcome " "to " "Ruby!"
=> "Welcome to Ruby!"

If you aren’t happy with the above options you can chain strings together using the << method:

myString = "Welcome " << "to " << "Ruby!"
=> "Welcome to Ruby!"

Still not enough choices for you? Well, how about using the concat method:

myString = "Welcome ".concat("to ").concat("Ruby!")
=> "Welcome to Ruby!"

Freezing a Ruby String

A string can be frozen after it has been created such that it cannot subsequently be altered. This is achieved using the freeze method of the String class:

myString = "Welcome " << "to " << "Ruby!"
=> "Welcome to Ruby!"


myString << "hello"
TypeError: can't modify frozen string

Accessing String Elements

Fragments of a Ruby string can be accessed using the [] method of the String class. One use for this method is to find if a particular sequence of characters exists in a string. If a match is found the sequence is returned, otherwise nil is returned:

myString = "Welcome to Ruby!"

=> "Ruby"

=> nil

Pass an integer through to the [] method and the ASCII code of the character at that location in the string (starting at zero) will be returned. This can be converted back to a character using the chr method:

=> "c"

You can also pass through a start position and a number of characters to extract a subsection of a string:

myString[11, 4]
=> "Ruby"

You can also use a Range to specify a group of characters between start and end points:

=> "Welcome"

The location of a matching substring can be obtained using the index method:

=> 11

Comparing Ruby Strings

It is not uncommon to need to compare two strings, either to assess equality or to find out if one string is higher or lower than the other (alphabetically speaking).

Equality is performed either using the == or eql? methods:

"John" == "Fred"
=> false

"John".eql? "John"
=> true

The spaceship (<=>) method can be used to compare two strings in relation to their alphabetical ranking. The <=> method returns 0 if the strings are identical, -1 if the left hand string is less than the right hand string, and 1 if it is greater:

"Apples" <=> "Apples"
=> 0

"Apples" <=> "Pears"
=> -1

"Pears" <=> "Apples"
=> 1

Case Insensitive String Comparisons

A case insensitive comparison may be performed using the casecmp method which returns the same values as the <=> method described above:

"Apples".casecmp "apples"
=> 0