background preloader

Ruby

Ruby
Related:  Ruby

What is the difference between require_relative and require in Ruby? 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!"

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

DPiR | Companion site to Russ Olsen's book 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

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!

GitHub - sdsykes/fastimage: FastImage finds the size or type of an image given its uri by fetching as little as needed Struggling With Ruby: Ruby GUI I have been looking at how easy it would be to put a GUI front-end on Ruby. Ruby has no native support for a GUI (one place Java and C# really win over Ruby), and while Rails has become the standard for web aplications, there are numerous options for a GUI plug-in. I have had a look at Tk, Fox and Swing (for JRuby). Recently, however, I found Ruby Shoes. Shoes is a very simple GUI, but that is part of its appeal. I doubt it has the comprehensive range of widgets that Tk does, for instance, but it can cope with JPEGs, which Tk cannot (as far as I could find). Unlike most other GUI toolkits, Shoes is not just a Ruby front end to an existing kit, which seems to make it feel more Ruby-like. However, the big problem with Shoes is that there is no menu support incorporated. Find Shoes here: My Quick Guide to Shoes Everything in Shoes goes inside a Shoes.app block (usually, anyway). Shoes.app doend flow doend @gui_completed = stack@gui_completed.clear

DateTime (Ruby 2.1.1) _strptime(string[, format='%FT%T%z']) → hash click to toggle source Parses the given representation of date and time with the given template, and returns a hash of parsed elements. _strptime does not support specification of flags and width unlike strftime. See also strptime(3) and strftime. static VALUE datetime_s__strptime(int argc, VALUE *argv, VALUE klass) { return date_s__strptime_internal(argc, argv, klass, "%FT%T%z"); } civil([year=-4712[, month=1[, mday=1[, hour=0[, minute=0[, second=0[, offset=0[, start=Date::ITALY]]]]]]]]) → datetime click to toggle source Creates a date-time object denoting the given calendar date. DateTime.new(2001,2,3) DateTime.new(2001,2,3,4,5,6,'+7') DateTime.new(2001,-11,-26,-20,-55,-54,'+7') commercial([cwyear=-4712[, cweek=1[, cwday=1[, hour=0[, minute=0[, second=0[, offset=0[, start=Date::ITALY]]]]]]]]) → datetime click to toggle source Creates a date-time object denoting the given week date. DateTime.httpdate('Sat, 03 Feb 2001 04:05:06 GMT') DateTime.now

Self adjusting windows in shoes on 2013-05-21 00:54 Is there a way with *shoes* to have a windows that will "grow" according to the information drop on it so the user does not have to scroll? The stmt: Shoes.app :width => 900, :height => 720 creates a fix size windows. I would like to have the windows expand as information is dropped on it. Thank you on 2013-05-22 13:17 on 2013-05-22 15:29 Ashbb, Thank you for your help. I find FXRuby a better option in this particular case, you can consult many things about the widget, judge how many rows were wrote and by knowing that resize the widget until you have only one row, or you can do many many things. on 2013-05-23 15:25 Damin, You are likely correct. Please log in before posting.

Net::HTTPResponse (Ruby 2.3.0) body() click to toggle source Returns the full entity body. Calling this method a second or subsequent time will return the string already read. http.request_get('/index.html') {|res| puts res.body } http.request_get('/index.html') {|res| p res.body.object_id p res.body.object_id } body=(value) click to toggle source Because it may be necessary to modify the body, Eg, decompression this method facilitates that. def body=(value) @body = valueend read_body(dest = nil, &block) click to toggle source Gets the entity body returned by the remote HTTP server. If a block is given, the body is passed to the block, and the body is provided in fragments, as it is read in from the socket. Calling this method a second or subsequent time for the same HTTPResponse object will return the value already read.

Related: