background preloader

Ruby

Ruby
Related:  Ruby

What is the difference between require_relative and require in Ruby? Data on Rails - Google Data Protocol - Google Code Eric Bidelman, Google Data APIs Team February 2009 Contents Introduction "Where's Ruby on the list of client libraries?" Motivated by the ferocious appetite of our developers and the enduring popularity of Ruby on Rails (RoR), my colleague Jeff Fisher has forged a Ruby utility library from the fiery depths of Mount Doom. Mind you, it's not a full-blown client library, but it does handle the fundamentals like authentication and basic XML manipulation. Audience This article is intended for developers interested in accessing the Google Data APIs using Ruby, specifically Ruby on Rails. Getting Started Requirements Ruby 1.8.6 patch level 114+ downloadRubyGems 1.3.1+ downloadRails 2.2.2+ download Installing the Google Data Ruby Utility Library To obtain the library, you can either download the library source directly from project hosting or install the gem: sudo gem install gdata Tip: For good measure, run gem list --local to verify that the gem was installed properly. Authentication ClientLogin AuthSub #!

John Ewart In Ruby, interfaces are not really a part of the programming paradigm. By its very nature, Ruby is designed to be flexible and loosely-typed, and that provides developers with a lot of power. Over the years I’ve seen a number of projects that are written in Ruby become internally inconsistent. What I mean by this is that developers decide that their abstractions or structure isn’t working and instead of working from the bottom up, just start writing modifications to make things work. Take, for example, Fog.io, which I mentioned in a previous post. Just because Ruby does not formally support interfaces in the same way Java or C# do, doesn’t mean that it is impossible to maintain a set of interfaces in your code and ensure that developers are adhering to those interfaces. Take for example, some code in Ruby that looks like this: An example interface in Ruby class FunctionalInterface def do_thing raise "This is not implemented!"

showing google calendar events in rails view Ruby Exceptions Raising An Exception An exception is a special kind of object, an instance of the class Exception or a descendant of that class that represents some kind of exceptional condition; it indicates that something has gone wrong. When this occurs, an exception is raised (or thrown). By default, Ruby programs terminate when an exception occurs. But it is possible to declare exception handlers. An exception handler is a block of code that is executed if an exception occurs during the execution of some other block of code. Ruby has some predefined classes - Exception and its children - that help you to handle errors that can occur in your program. Reference: The above figure is from the Programming Ruby book. The chart above shows that most of the subclasses extend a class known as StandardError. The following method raises an exception whenever it's called. def raise_exception puts 'I am before the raise.' The output is: The raise method is from the Kernel module. Handling an Exception

Ruby sample code | Viadeo API You can find below a sample code implementing OAuth2 and an access to the ‘/me’ endpoint. The script will redirects you to the Viadeo OAuth2 authentication page, just enter some Viadeo credentials and you will be redirected to ‘ with the accesss_token set. This samples runs with Ruby 1.8 + Rails 2.3.4. Note: Error catching is here minimal, you have to implement a better error management.

DPiR | Companion site to Russ Olsen's book Full text search in in Rails with Sunspot and Solr « TechBot The book you should get to dig deeper into Solr Click here if you want to see a PDF version of this tutorial. Full source code for this tutorial is available at GitHub. Everyone wants to take their databases to run everything as fast as possible. When querying for text in our databases, we’re often doing “LIKE” searches. The database would be able to optimize this query and use the index to find the expected row. Your DBA getting ready to hit you Database indexes usually match from left to right, so, unless you have a nasty trick under your sleeve, this query will just look at ALL the rows in the products table and perform a match on every “name” column before returning a result. That’s where full text search based solutions come in for help. In this tutorial you’ll learn how to add full text searching capabilities to your Rails application using Sunpot and Solr. The source code for this example application is available at GitHub here . Starting the engines Listing 1 – sunspot.yml Searching

Net::HTTP (Ruby 2.0.0) An HTTP client API for Ruby.¶ ↑ Net::HTTP provides a rich library which can be used to build HTTP user-agents. For more details about HTTP see [RFC2616](www.ietf.org/rfc/rfc2616.txt) Net::HTTP is designed to work closely with URI. URI::HTTP#host, URI::HTTP#port and URI::HTTP#request_uri are designed to work with Net::HTTP. If you are only performing a few GET requests you should try OpenURI. Simple Examples¶ ↑ All examples assume you have loaded Net::HTTP with: require 'net/http' This will also require ‘uri’ so you don’t need to require it separately. The Net::HTTP methods in the following section do not persist connections. Net::HTTP.get('example.com', '/index.html') GET by URI¶ ↑ uri = URI(' GET with Dynamic Parameters¶ ↑ uri = URI(' res = Net::HTTP.post_form(uri, 'q' => 'ruby', 'max' => '50') puts res.body POST with Multiple Values¶ ↑ How to use Net::HTTP¶ ↑ uri = URI(' Response Data¶ ↑ Proxies¶ ↑ 1xx 2xx 3xx

Cassandra vs MongoDB vs CouchDB vs Redis vs Riak vs HBase comparison :: KKovacs While SQL databases are insanely useful tools, their monopoly in the last decades is coming to an end. And it's just time: I can't even count the things that were forced into relational databases, but never really fitted them. (That being said, relational databases will always be the best for the stuff that has relations.) But, the differences between NoSQL databases are much bigger than ever was between one SQL database and another. This means that it is a bigger responsibility on software architects to choose the appropriate one for a project right at the beginning. In this light, here is a comparison of Open Source NOSQL databases: The most popular ones # Redis # Best used: For rapidly changing data with a foreseeable database size (should fit mostly in memory). For example: To store real-time stock prices. Cassandra # Best used: When you need to store data so huge that it doesn't fit on server, but still want a friendly familiar interface to it. MongoDB # ElasticSearch # CouchDB # Accumulo #

String (Ruby 2.2.0) str % arg → new_str click to toggle source Format—Uses str as a format specification, and returns the result of applying it to arg. If the format specification contains more than one substitution, then arg must be an Array or Hash containing the values to be substituted. static VALUE rb_str_format_m(VALUE str, VALUE arg) { VALUE tmp = rb_check_array_type(arg); if (! str * integer → new_str click to toggle source Copy — Returns a new String containing integer copies of the receiver. integer must be greater than or equal to 0. "Ho! str + other_str → new_str click to toggle source Concatenation—Returns a new String containing other_str concatenated to str. "Hello from " + self.to_s str << integer → str click to toggle source str << obj → str Append—Concatenates the given object to str. a = "hello "a << "world" a.concat(33) string <=> other_string → -1, 0, +1 or nil click to toggle source nil is returned if the two values are incomparable. <=> is the basis for the methods <, <=, >, >=, and between? tr!

Related: