What is the difference between require_relative and require in Ruby? Ruby Historique[modifier | modifier le code] Depuis l'arrivée d'une documentation anglophone, et du framework web Ruby on Rails en 2004, Ruby a connu un certain engouement qui n'a cessé de croître jusqu'en 2008 dans le monde de la programmation, puis une érosion lente si on en juge par son index Tiobe. Philosophie[modifier | modifier le code] Ruby est fortement orienté objet et se rapproche ainsi du paradigme objet de Smalltalk[note 1] : Fonctionnalités[modifier | modifier le code] Les fonctionnalités principales sont : Implémentations[modifier | modifier le code] Ruby est fourni avec irb, un interpréteur de commandes interactif pour tester en profondeur le fonctionnement du langage. Depuis le 1er janvier 2007, le développement de Ruby (1.9) est basé sur l'interpréteur YARV écrit par Koichi Sasada[10]. Outre YARV, il existe plusieurs autres interpréteurs Ruby[11] : La version 2.0 permet de créer des applications graphiques pour macOS et iOS. Interprètes embarqués[modifier | modifier le code]
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!"
Lancez-vous dans la programmation avec Ruby Ce cours est destiné à tous les curieux qui souhaitent s’initier à la programmation, et aux codeurs qui souhaitent apprendre un nouveau langage. Ici, vous allez découvrir Ruby, un langage de programmation qui a été créé par un informaticien japonais dans les années 1990, avec un objectif en tête : faciliter la vie des développeurs avec une syntaxe et des outils simples et agréables à utiliser. Que vous ayez des connaissances en programmation ou pas, vous pouvez suivre ce cours où vous apprendrez à manipuler les outils de base de Ruby ainsi que ses puissants "objets". Une fois que vous maîtriserez ces notions, vous aurez toutes les cartes en main pour aller plus loin et réaliser vos propres projets, comme par exemple :
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
Club des développeurs Ruby et Ruby on Rails : actualités, cours, tutoriels, programmation, codes sources, livres, outils et forums DPiR | Companion site to Russ Olsen's book Xavier Nayrac 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
Ruby and the Sinatra framework Introduction DSL vs GPLGitHub RepositorySinatraHostingExample RequirementsSet-upShotgunLoading pagesLoading templatesStatic resourcesHandling errorsPerformanceHosting our applicationConclusions Introduction In my opinion there's isn't enough good information on the web about how to get started building a Ruby based website (unless you want to use the ever popular Ruby on Rails framework). But even then, it's not as straight forward as you'd think (not for someone new to the language or server-side coding in general). DSL vs GPL Before we go any further, it's worth clarifying the difference between a Domain Specific Language (DSL) and a General Purpose Language (GPL). If we look at a DSL language such as PHP**: it is very easy to build a site using PHP because you can just input some procedural code, upload the file onto a server that supports PHP (and what server doesn't nowadays) and that's all there is to it (! GitHub Repository Sinatra So, let me begin by introducing you to Sinatra. Hosting
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!