Posts tagged ruby

Custom File Processing Forms in Active Admin

2016-01-18T00:00:00.000Z

I needed to add a form to Active Admin so that a client could upload files, which would be parsed and the data added to the database. Here’s how I did it. In , register the relevant model with ActiveAdmin. In this case, the files contain information of users, which would be loaded into the database, so we park it under the User model. Then, we add an to the User panel, and associate that with a , which we use to render a form partial. We indicate the action in the form partial, which is added as below: This action can contain any arbitrary parsing application logic, the result of which can be displayed to the user in a flash notice, as demonstrated simply above. Also note that the file is not stored subsequently.

Single Table Inheritance in Rails

2015-06-27T00:00:00.000Z

Single table inheritance (STI) in Rails allows you to store Ruby subclasses in the same database table. Let’s get started with a brand-new Rails project to learn what that means. I’ll be using Rails 4.2.3 and SQLite as the database. First, an empty User model: In : Generate a migration for it. To implement STI, we add a column called of type to the class. Let’s also have a name column: Migrate the SQLite database: Fire up the Rails console: Let’s create a new user: No problems so far. Now, let’s say we want to differentiate users between regular users and power users. At this juncture, let’s pretend we don’t know about STI. Since we’ve already created the column, let’s use that, so we can find different types of users by writing something like this: Go ahead and create such a user: Uh oh. What just happened? Why is Active Record complaining about not being a subclass of ? It turns out that Active Record, upon, seeing a column named , automatically assumes you’re implementing STI. Into Rails Down The Stack Let’s take a dive into the Rails source code to find out how Active Record automatically assumes this behaviour, by finding out step by step what happens when we attempt to type to create a new power user. The source code below is as of commit on the branch. Sean Griffin explains briefly in his comments in Active Record’s module. In : method in : We start our investigation from Active Record’s module, which contains the method definitions of some of the most-commonly used Active Record methods, such , , , and (protip: use in Sublime Text to search by method definition). We first check if the supplied argument is an array. If so, we recursively call for each member in the array. This means you can do something like: and Active Record will create three users, and even return them to you in the same array structure you specified: If we supply a hash, then the method is called: method in : Notice the splat operator, which converts all but the last argument into an : We take the first member of the array and run the method on it, which is located in the same file, some ways down: method in : This method checks through all of the attributes in the model to see if any of their names match the specified inheritance column, which in this case is . Therefore, returns . Let’s see where and come from. method in : where is: method in : I’m going to stop here because steps into whole new territory: namely, into the scary module…

Notes on "Rebuilding a Web Server"

2015-07-16T00:00:00.000Z

Some notes I took while watching Rebuilding a Web Server, a brief walkthrough by Marc-André Cournoyer on writing a simple Rack-compliant web server. The code for the class is here. Concurrency The entire stack looks like this: There’s also a scheduler running alongside, handling concurrent connections. Such a scheduler can be implemented in different ways: threads, pre-forked processes, or an event loop. Threads A naive implementation would look like this, spawning a new thread for each incoming socket connection: Web servers like Puma use threads. Thread spawning is quite expensive, so web servers that use threads for concurrency will usually spawn a number of threads (thread pool) on bootup and reuse them. Pre-forked Processes Preforking is a popular concurrency model used by servers such as Unicorn and Nginx. creates a copy of the current process, and this child process is attached to its parent process. The two of them share the same socket. Worker processes are forked beforehand, and all of them share the same listening socket. Whichever process is free will be scheduled by the OS scheduler to handle the next incoming connection on the socket. Presumably, leveraging on the OS scheduler is really efficient. Event Loop We can simulate an event loop in Ruby using a gem called . is a feature-packed gem, and comes with helper methods that handle accepting, reading and writing to and from socket connections for us. is an instance method of the IO class in Ruby which allows us to read data off a socket as soon as data is available. The APIDock entry on elaborates further: readpartial is designed for streams such as , , , etc. It blocks only when no data immediately available. This means that it blocks only when following all conditions hold. the byte buffer in the IO object is empty. the content of the stream is empty. the stream is not reached to EOF. Using the method, we can read off a socket like this: is a method with similar functionality. is a gem that wraps around Node’s HTTP parser. Rack Rack is a set of specifications that web servers, middleware applications, and application frameworks must adhere to. Rack apps must have a single point of entry named , which must return an array containing the status code, the headers, and the body of the response. Things which behave exactly like Rack tells them to (e.g. Unicorn, Rails) are Rack-compliant, and the benefit of this is that Rack-compliant things can be used in conjunction, layered on top of…

Rails Boot Sequence (Part 1)

2015-07-17T00:00:00.000Z

Today, we investigate Rails’ boot sequence by observing what happens when we run . Part 2 will look at . Github links to relevant files are provided as necessary. Our journey begins inside the binary, which is executed by : It calls , which corresponds to : In : is in charge of executing the inside your Rails application. It will look for it recursively, meaning that you can call anywhere in your application directory. In fact, or is equivalent to calling or See the abridged contents of below: Next, we turn our focus temporarily to your Rails application. In , two files are required: (in your app directory) determines the location of the Gemfile and allows Bundler to configure the load path for your Gemfile’s dependencies. parses options passed in as command line arguments, including alias mapping ( for , for , etc.) is in charge of throwing errors in the case of invalid commands, or delegating valid commands to the respective methods, themselves split into files in the directory: For example, if is run, the method in requires and runs the class method from the class, passing it your application as the first argument ( is made known of your application by requiring , which you’ve kindly provided previously in ): In , you can see the class method instantiating itself and calling the new instance’s instance method: As it is instantiated, is set as your Rails application, and is set to if present, or defaults to : Let’s see if the above code actually works by setting your application config to use as the console instead: Great success! Now let’s look at the actual instance method, whose code is relatively self-explanatory: Finally, boots the console. Next, we’ll look at the code path taken by . As indicated in the comments, this file is auto-generated by RubyGems. How does it know to load Rails, as in the last line ()? Taking a look in gives us the answer: What does the above mean? RubyGem’s documentation: Take a look inside the directory - its contents will be very familiar soon :) The binary is defined by the sha-bang to be executed by , which is a thin wrapper that allows RubyGems to run initialization hooks () before Ruby runs the actual code (). This is what the actual binary looks like: You can test this out for yourself with just 3 lines of code. Create a file with the following: Run it and see what happens:

OpenSSL woes with Ruby 2.2.3 and rvm

2015-11-30T00:00:00.000Z

If anyone is getting any errors along the lines of the following: for any reason at all (/, , etc.) after upgrading to Ruby 2.2.3 with , reinstalling it from source fixes it: Another one of those supremely obscure bugs. I hope this saves a few hours/days of desperate googling/stackoverflowing.

Adding MiniMagick support to carrierwave-video-thumbnailer

2015-12-05T00:00:00.000Z

TLDR: I forked and added MiniMagick post-processing support. Using good ol’ Carrierwave to upload video files to S3, I came across a need to generate video thumbnails. A quick Google revealed , which worked perfectly. However, I needed to rotate the thumbnails (90 degrees clockwise) before uploading them. I thought it would be trivial, but it turned out that Carrierwave’s DSL made using multiple processes in a version block intractable. I couldn’t find a reasonable way to proceed from here, and after spending 2-3 hours trying different permutations of the above, wrangling Carrierwave’s DSL and trying to get things to work, I finally dug into the gem to see if I could get MiniMagick to work inside of the call. To expose as much of MiniMagick’s API as possible, I opted for the user to pass in a Proc. To use it, simply pass in a Proc as : You can look a look at my commit here for more detail on how I implemented the change. For some reason, the thumbnails that were generating from portrait iOS videos were rotated 90 degrees counter-clockwise. Some others have faced this issue as well.

Invalid Gemspec

2016-01-06T00:00:00.000Z

A minor, but frustrating bug. While working on Rogger, I came across the following problem while bundling my local Rogger into a test project: I was quite confused. I checked the gem file but nothing inside seemed like a reference to . I tried building the gem: The same error happened. It turns out this happens because the gets the list of files from the command. I happened to check the gem file in, which was why this error was occurrring. Removing it, then, fixed it:

Rogger 0.1.2 Released

2016-01-07T00:00:00.000Z

Rogger v0.1.2 has been released - a patch that makes logging to Graylog2 even easier. For example, in a Rake task you can do something like: Notably, this is also the first time ever that I’m using metaprogramming in actual code. The actual bit is below: This bit defines the , etc. module-level methods that you see above using , encapsulating singleton’s methods as module methods instead. These levels correspond to Rails log levels and are used elsewhere in as well, so Rogger takes advantage of that. The user, then, does not have to deal with the actual GELF logger instance, as is already the case pre (Rogger hooks onto the Rails logger by using ).

How Ruby Objects Are Implemented

2016-01-29T00:00:00.000Z

Next post: How Ruby Classes are Implemented I’m currently reading Pat Shaughnessy’s excellent book Ruby Under a Microscope, and these are notes that I’ve summarized from the chapters I’m currently going through. This post, and the next, are notes from Chapter 6, Objects and Classes. It can be a bit confusing to describe the content purely in words, but the book itself contains many helpful diagrams, so pick it up if you’re interested! Every Ruby object is the combination of a class pointer and an array of instance variables. Pat Shaughnessy A user-defined Ruby object is represented by a structure called an , and is referred to by a pointer called . Inside , there is another structure called , which all Ruby values will have. Aside from the structure, also contains , a count of how many instance variables the object has, , a pointer to an array of values of the instance variables, and , which is a pointer to a hash table stored in the object’s associated structure that maps the name/identity of each instance variable to its position in the array. In this case, the RObject representing will have a of 2, and its will be a pointer to an array containing the values and . The RObject representing will have a of 1, and its will be a pointer to an array containing just . Both and will have a structure whose pointer references the same Fruit structure. Generic Objects (, …) Generic objects such as strings and arrays are represented by more specialized versions of , called , , etc. Their internal representations are more optimized for the kind of values they store. An example of this optimization is the presence of , which is an array of a certain fixed size. This array will be used to store the values of the instance variables in the structure itself if they fit, instead of allocating memory for and referencing an external array. They also contain the structure. contains a few internally-used flags and a pointer to its associated class, called . Classes are represented by a structure, which is discussed in the next post. Simple Values Simple values like (small) integers, , and do not have an associated -like structure. Instead, their value is stored directly in itself. The identity of these values are indicated by different flags in (do not confuse the flags in with those in , they are different). For example, if the is 1, then Ruby knows to intepret the rest of as an integer value instead of a pointer address to its associated (or etc…

Preprocessing in Searchkick

2016-02-04T00:00:00.000Z

Coming back to Jekyll from Hugo, I’d grown accustomed to shortcodes, which are awesome to keep your Markdown source as HTML-free as possible. You can emulate shortcodes with custom liquid tags! In your folder, create a new file, and then write a class that inherits from . Let’s see an example below of a tag for embedding Youtube videos: The general thing to take note of here is that whatever appears after the tag name will be stringified and sent to the second argument of , so for example, a custom tag that looks like: in the method above will be (note the trailing whitespace). There’s no further treatment, so you will have to parse the string yourself somehow to identify what is what, and then assign what you need to instance variables: The class must also implement a method, which returns a string representation of the HTML, so in this case of Youtube embeds: And voila! You can now use your Youtube tag like so: Great success 👍

How Ruby Classes Are Implemented

2016-01-10T00:00:00.000Z

Previous post: How Ruby Objects are Implemented Ruby classes are represented by a structure. It’s a fairly big structure, so I’ve broken it down according to what a Ruby class actually does and what it contains. A Ruby class is a Ruby object that also contains method definitions, attribute names, a superclass pointer, and a constants table. Pat Shaughnessy A Ruby class contains method definitions. Method definitions are stored in a method table. In Fruit, this will be a table that contains the keys , , , and , and whose values are pointers to the actual method definitions and their YARV instructions. A Ruby class contains instance-level attribute names. As mentioned in the previous post, a contains a pointer which is a hash table that maps attribute names to their positions in the array. In Fruit, this will be a hash table that contains the keys and , and whose values are their indices in the array. A Ruby class is also a Ruby object. If you print out Fruit’s class, it shows Fruit as being an instance of the class. A Ruby class is also a Ruby object. As a Ruby object, a Ruby class will also have: A class pointer that references the of its class (and is stored, like any other , in a structure) Its own methods (called class methods - those that you define with ). These methods are not stored in the class itself, but in its metaclass, or sometimes called the singleton class. This singleton class is what is set to, not Class. A table of its own class-level instance variables called A Ruby class has a pointer to a superclass, which allows it implement inheritance. A Ruby class will have a pointer that references its superclass. In this case, Pear will have a pointer referencing Class, and a pointer referencing Fruit. There is a dinstinction between the two - refers to the class from which the Pear class object is instantiated, whereas refers to Pear’s logical superclass. The Pear class object is not instantiated from Fruit, but Class. A Ruby class contains constants. A Ruby class can contain constants. Constants are stored in a constants table. Ruby classes are also objects. When a class is created, Ruby actually creates two objects - the class object itself, as well as a metaclass object. Class Variables In Ruby, there are class variables, which are dintinct from class-level instance variables, as mentioned above. Class-level instance variables are defined with a single prepended to the variable name and are scoped to each individual class…

How Closures Actually Work

2016-01-25T00:00:00.000Z

Like objects, closures are a mechanism for containing state. In JavaScript, a closure is created whenever a function accesses a variable defined outside the immediate function scope. It’s easy to create closures: Simply define a function inside another function, and expose the inner function, either by returning it, or passing it into another function. The variables used by the inner function will be available to it, even after the outer function has finished running. Eric Elliott, The Two Pillars of JavaScript — Pt 2: Functional Programming Most online articles on closures start with a definition that resembles something like the above. It’s a description of its behaviour and how to create one. What most of them (read: all of them) fail to do is explain how closures are actually implemented, and then, why they behave the way they do. I believe this is important for truly understanding closures, and eventually, why closures are as important as they are. This is a post on how closures are implemented in Ruby, and it is directly inspired from Pat Shaughnessy’s excellent book Ruby Under a Microscope. I also wrote 2 other posts as notes while going the book: How Ruby Classes Are Implemented and How Ruby Objects Are Implemented. A closure is a data structure that contains a lambda expression, which is a function that takes a set of arguments, and an environment to be used when the lambda expression is invoked. Closures in Ruby can be created in a few different ways. Here, a closure is created using a block: When the first line is executed, Ruby first creates a structure representing the string on the heap, and then pushes a reference to the onto its internal stack, in the current stack frame. At the same time, the current on the Ruby call stack also has a pointer (EP stands for Environment Pointer) referencing the current stack frame: When the second line is executed, the Ruby tokenizer has already determined that a block is present to be passed to the method as an argument. YARV pushes a C structure representing the block, called , onto the call stack. contains a few things: is the value the self pointer in the block’s environment. refers to the class of the current object. is identical to the pointer found in the preceding , which references the current internal stack frame. is a pointer to the compiled YARV instructions that correspond to the Ruby code inside the block, which in this case is: If we disassemble the line above, we can see the…