Winforms : Programmatically Updating BindingSource
textBox.Text = “Foo”;
textBox.DataBindings[0].WriteValue();
wallacepolsom


Discoholic 🪩
I'd rather be in outer space 🛸
cherry valley forever
Lint Roller? I Barely Know Her
Jules of Nature
"I'm Dorothy Gale from Kansas"

oozey mess

❣ Chile in a Photography ❣
RMH


Kaledo Art
Peter Solarz
Claire Keane

@theartofmadeline
he wasn't even looking at me and he found me
NASA

PR's Tumblrdome
seen from South Africa
seen from Germany
seen from Brazil

seen from Saudi Arabia

seen from T1

seen from T1
seen from China
seen from Poland

seen from United States

seen from United States
seen from Türkiye

seen from Germany

seen from United States

seen from Angola
seen from Russia

seen from United States

seen from Spain
seen from United States

seen from Germany
seen from Belgium
@a-rne
Winforms : Programmatically Updating BindingSource
textBox.Text = “Foo”;
textBox.DataBindings[0].WriteValue();

Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
Free to watch • No registration required • HD streaming
Edit Windows 8.1+ System Path
No, being Administrator is not enough. Run this from the command line with admin privileges :
rundll32.exe sysdm.cpl.EditEnvironmentVariables
You may now edit your system path.
IIS : HTTP Error 500.19 - Internal Server Error
Spun up my ASP.NET MVC app and saw this :
This configuration section cannot be used at this path. This happens when the section is locked at a parent level. Locking is either by default (overrideModeDefault="Deny"), or set explicitly by a location tag with overrideMode="Deny" or the legacy allowOverride="false".
Config File \\?\C:\Users\me\myCode\myApp\web.config
Config Source: 163: <modules />
My <modules> section in web.config wasn’t actually empty, but that doesn’t matter for the sake of this example.
I attempted a bunch of fixes, including granting the DefaultAppPool user read permissions, enabling various IIS Windows features and standing on one leg while reciting the alphabet backwards.
Turns out the error message is actually perfectly on point, but I just didn’t know what to look for - or more importantly, where these settings were being defined. The defaults are set in the applicationHost.config file in the IIS installation directory :
%windir%\System32\inetsrv\config\applicationHost.config
I updated the <modules> section as follows :
<section name="modules" overrideModeDefault="Allow" />
Note that, because nothing can ever be simple, the applicationHost.config file needs to be edited with a 64-bit editor. I used Notepad.
WCF Service Update Fail
This happened when attempting to update a WCF service reference :
‘http://localhost/MyService/Service.svc’. The request failed with HTTP status 404: Not Found. Metadata contains a reference that cannot be resolved: 'http://localhost/DataServices/DataService.svc'. The remote server returned an unexpected response: (405) Method Not Allowed. The remote server returned an error: (405) Method Not Allowed.
After hours of trial and error, this resolved it : Windows Features -> .NET Framework 4.5 Advanced Services -> WCF Services -> (check) HTTP Activation -> OK
VirtualBox Freeze on FullScreen [Linux]
Try disabling the mini toolbar. I have no idea why this works :
Settings -> User Interface -> (uncheck) Mini Toolbar : Show in Full-screen/Seamless

Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
Free to watch • No registration required • HD streaming
Mount Windows CIFS share from Linux
sudo mount -t cifs -o username=username,password=password,domain=domain //127.0.0.1/sharedirectory ~/windows-share
Replace ‘username’, ‘password’ and ‘domain’ with the appropriate values, and replace the localhost IP and ‘sharedirectory’ with that of the server and directory you’re trying to mount
Helpful Vimperator Tweaks
:noremap j 7j :noremap k 7k :highlight Hint font-size:135%;color:white;background-color:red;padding:2px; :set hintchars=hjklasdfgyuiopqwertnmzxcvb
Another Day, Another Cryptic Compiler Error
I kind of feel like interpreting opaque compiler warnings is a defining part of life as a programmer. It’s not really about writing code, it’s about acting as a sort of confused medium for the spirit of computation.
So today, I added an assembly that unbeknownst to me targeted a newer .NET version (in this case, 4.5.1) than that of the project I was adding it to (4.5).
First thing I noticed was that a little yellow warning triangle appeared next to the reference in the solution explorer. I should have probably investigated this visual cue, but I chugged along undaunted. This was a mistake.
Anyhow, when I added my using directive (using FooSpace), IntelliSense didn’t complain. My heart rejoiced, and I tried to build. Alas, tragedy struck :
The type or namespace name 'FooSpace' could not be found (are you missing a using directive or an assembly reference?)
Derp. The error originated from my using directive, which is now underlined with a blue squiggly instead of the usual schoolteacher red. That’s interesting. Is my assembly only kind of missing? Mostly dead?
The compiler error itself is pretty useless, since it’s vague to the point of inaccuracy. I should have checked the compiler warnings, but instead looked up the meaning of the warning symbol next to the reference. As it turns out, it commonly indicates version incompatibility.
In my case, the fix itself was breezy. I didn’t have to modify any actual code in my 4.5.1 project, only the target framework in the project’s properties. Presto, it worked.
Lesson learned, being more attentive to warnings could have saved me time. Could also stand to develop a better intuition for Visual Studio. In addition to the compiler warnings, I missed a tooltip in the solution explorer that would have steered me on the right track.
Reference :
Stack Overflow : Reference a .NET framework 4.5.1 assembly in a 4.0 project Version Compatibility in the .NET Framework
Killing *Nix Processes by Search Term
What I wanted to do : kill -9 processes by search term
Why I wanted to do it : Chrome. It’s always Chrome.
What sucks about it :
My Solution :
This had bugged me for a while, so it was nice to finally bandaid it up. What you see above is a super dirty Bash script that will take one string argument search term, gather a collection of all processes where that term appears (presumably in the COMMAND column), and then execute kill -9 on each PID that is returned.
I’m sure there are better ways of tackling this, but it works for me. Just make sure your search term is unique to the process you want to kill
Returning Binary Data from .NET MVC 4 Controller
I needed to write an API that would, among other things, read a binary image from a database and return it over HTTP.
I'm pretty new to .NET, and this was harder than I expected it to be. For instance, most google searches returned solutions intending to be implemented with the WebAPI controller, and not a hum-drum System.Web.Mvc.Controller.
Most solutions, thus, recommend returning an HTTP Response (System.Net.Http.HttpResponseMessage) with the binary data provided as a byte array (System.Net.Http.ByteArrayContent), as seen below. In both code samples, widget.Data contains a byte[] array
However, when using a stock MVC controller, this is the best I could get out of the server :
StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.ByteArrayContent, Headers: { Content-Type: image/png }
Basically, the browser wasn't able to decode the response. I honestly don't know why. I imagine WebAPI implements some middleware that formats the response object. Anyhow, based on what I'm seing on SO, implementing APIs using the WebAPI 2.0 controller (System.Net.Http.ApiController) is the current standard. However, this wasn't an option for me, so I needed to find out how to do this using a stock controller.
As it turns out, the solution is to return a File object (System.IO.File), with the byte array passed to the constructor as seen here.
At the end of the day, this was one of those straightforward bugs that cost me a few frustrating hours. Maybe this will save someone else that same time :P

Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
Free to watch • No registration required • HD streaming
Nano Book Review : The Clockwork Universe
Wat?
As an experiment, I plan to write short reviews of books I finish in a dual effort to share tidbits I found interesting as well as a means to help me internalize key concepts.
Most recently I finished "The Clockwork Universe: Isaac Newton, the Royal Society, and the Birth of the Modern World", by Edward Dolnick.
At a very high level, this book deals with the socio economic and cultural state of affairs at the time of the scientific revolution, as well as a few deep-dives into some of the key figures involved such as Keppler, Galileo, Leibnitz and of course Newton.
Hello, London
Dolnick begins by setting the stage, so to speak. In the context of the scientific revolution, that means describing in fairly morbid detail the state of society in late 17th century London. While specific events such as the Great Plague (1665) and the similarly named Great Fire (1666) are discussed, the entire point of these passages is to describe the prevailing attitudes of the time. A few points that stood out were the rampant criminality, a murder rate that rivals modern day Haiti, the crime of theft punished by branding of either hands or face and, of course, public executions as a form of entertainment. Most importantly, however, was discussion of the role of religion.
Religious Push and Pull
While I had a certain academic sense that religion was a Big Deal at the time, I freely admit I didn't really have a solid intuition as to quite how omnipresent it truly was. The fact, for instance, that Kepler was a devout astrologer who devoted the majority of his life to deciphering God's geometrical plan for the universe came as a surprise. While I tend to view religion and science as neatly separated disciplines, this was not always so. Dolnick posits that the influence of religion in the 17th century was so overarching that the notion of atheism was entirely foreign. As would be expected, religious institutions resisted challenges to their authority, perhaps most famously in the case of Galileo's promotion of a Heliocentric model of the solar system. However, there apparently existed an interesting flip-side to religious fervor of the era. Namely, the role of religion as a driving force of scientific accomplishment.
As mentioned, one of Kepler's central motivations apparently lay in exploring God's geometrical plan for the universe. Likewise, Newton himself was driven in large part by religious conviction, himself practicing a lifetime of celibacy and dedicating significant portions of Principia to reverent passages expressing his deeply felt awe at the wonders of God's creations. As an interesting side-note, this particular sense of awe and reverence with regards to the laws of nature was in some sense echoed centuries later by Einstein. In the case of the latter, it's important to note that his views were more generally spiritual than adhering to any formal religion or system of belief.
Aristotle Challenged
Despite featuring in the title, this isn't really a book about Newton, per se. By the time attention turns to his work in earnest, a considerable amount of time has already been devoted to the aforementioned Kepler, the formation of the Royal Society, Galileo and his work on falling bodies, classical Greek mathematics and the Aristotelian model of physics among several others.
The fairly significant time spent discussing classical Greek models of the universe was not incidental. Prior to the scientific revolution, classical works set much of the foundation of scientific and mathematical knowledge in Europe. An important question that was posed, and at least partially answered, was why did none of the major breakthroughs of insight that occurred in 17th century Europe occur in Greek antiquity? Undoubtedly, some of the breakthroughs that occurred in the 17th century were due to technological advances. Notably, the work of Kepler, Brahe and to a certain extent Galileo. However, many of Newton's key insights were borne out in quasi-isolation. This question bears special relevance to the discovery of calculus - developed in parallel by Leibniz and Newton. More directly : why did the Greeks not discover calculus?
The answer to this question was phrased as a matter of culture. Specifically, the author suggests that ancient Greeks held the view that Mathematics was the study of immutability and immutable perfection. Calculus can in this context be seen as antithetical to the Greek view, since it deals explicitly with change. In other words, the study of change violated the very purpose of mathematical study in ancient Greece. This, the author suggests, imposed a set of blinders on mathematical thought which were not effectively lifted until 17th century Europe.
The More Things Change
Returning to the 17th century, I was struck by a few key points. The first being the role of religion as a driving force and inspiration, and not solely a hindrance to scientific achievement. The second, being the contrast of our contemporary concept of physics to that of post-Principia Europe.
For instance, I personally find certain notions of Quantum physics counter intuitive and a little bewildering, to say nothing about more theoretical concepts like string theory. The notion that there is true randomness at the sub-atomic level is a strange concept that's incredibly difficult to reconcile with our observational experiences and "common sense".
What Dolnick convincingly argued was that the learned public in the late 17th and early 18th century viewed what we now call "classical", or Newtonian, mechanics with much of the same suspicion. For instance, Newton's work on gravity provided a description - not an explanation. He formed a mathematical model for physical behavior, but explicitly shied away from attempting to theorize about why. In fact, the point was made that gravity as a description alone feels incredibly lacking. In fact, it almost embodies the very notion of spooky action at a distance. Put plainly : how could gravity possibly effect changes across incalculable distances instantaneously? Without the work of Einstein, which helpfully begins framing a model for why, the description of gravity's effects does feel frustratingly bare.
These, I think, were the most interesting observations put forward by Dolnick. I've gained a slightly better appreciation for religion's complex role in the formation of modern society in both spurring and hampering scientific advance and discovery. In addition, I gained a slightly more informed appreciation of the not-so-distant past as the almost radically world it in fact was.
Not A TLDR;
I've obviously skipped over regurgitating the details of the publishing of Principia, Newton's feuds with other intellectuals and the odd yet interesting mix of science and superstition that permeated the scientific world at the time. It's not that these chapters weren't interesting (they were), but rather that the book does a much better job of detailing them than I could here. I personally found the book a worthwhile read, and would recommend it to others.
Reverse Engineering the JNI
The title is a misnomer, but the full description would is a little wordy. We're not actually reverse engineering the entire JNI framework, but rather a very specific subset of the conversions that occur between JVM data structures and their native counterparts (C, C++, etc.). Insofar as the intent behind this post is concerned, a more complete title might be :
C Programming Concepts : Reverse Engineering for (*fun)->profit
For starters, a little background. JNI stands for the Java Native Interface. Wikipedia contains a succinct description :
The Java Native Interface (JNI) is a programming framework that enables Java code running in a Java Virtual Machine (JVM) to call, and to be called by, native applications (programs specific to a hardware and operating system platform) and libraries written in other languages such as C, C++ and assembly.
Basically, (the) JNI is used when calling "lower-level" code from an application running on the JVM, as well as the other way around. I recently implemented a simple matrix calculation web API that calls native C code from a Java web-app. The code can be found on GitHub, and it's the work that prompted me to write this post.
Why JNI?
I'm intrigued by the mechanics involved in managing the relationship between a higher level environment (such as Java) and the lower level world of C. Being new to both, reverse engineering the JNI conversion functions is a great introduction to some basic C programming principles. Above all, it can help de-mystify some of the abstraction underpinning higher level languages & frameworks.
I want to emphasize that this is first and foremost fun and exploratory. Developing in C is not my day job. If you find any errors, please let me know and I will fix them promptly.
Pointer, Deconstructed
We now know in general terms what the JNI is and used for. Let's look at a specific application and use case. This is lifted straight out of the matrix-path application I referred to earlier, and is the actual code which performs a conversion from an array passed in from Java-land (jintArray matrix_values) and is converted to a jint structure which can be used by the C application (jint *matrix_ptr).
Objective : "convert" a Java array (object) into a data structure C will recognize.
A lot of the heavy lifting here appears to revolve around the env pointer. From the code alone all we can determine is its type (JNIEnv). Further reading is required :
Per Wikipedia :
The env pointer is a structure that contains the interface to the JVM. It includes all of the functions necessary to interact with the JVM and to work with Java objects.
To be precise (or pedantic), the env pointer is a pointer to (a pointer to) a structure which contains the interface to the JVM. More about that later. Before we can replicate any functionality, we first need to dig a little deeper.
Analyzing line 6, in particular, opens the door to a number of fascinating C paradigms. For instance, env is a perfectly acceptable variable name. However, we can see env variously referenced as being preceded by an asterisk, as well as being enclosed in parens, *env and (*env) respectively. What does this signify?
Let's deconstruct it :
We begin with env, previously introduced on line 2 with a data type of JNIEnv
Immediately, we know it is a pointer, since it is identified as such by being preceded with a single asterisk
Back on line 6, we can see that *env is now encircled in parens. The parens are only present to isolate the action occurring within, namely the single asterisk , meaning we are accessing its pointee, which is to say following the pointer to whatever lies at the memory location it points to.
The -> is arrow (or pointer) notation. This notation is used to access the member of a struct referenced by a pointer. This is an important observation, as it means that the de-referenced value of *env is also a pointer.
GetIntArrayElements (a function) is the pointee, and takes 3 parameters : the env pointer itself, the jintArray (JVM) array, and a third param which we will ignore (tldr; it's an output parameter which indicates whether the return value is copied or "pinned")
Please see below for a quick illustration of pointer (to pointer) declarations. The ampersand (&) indicates that we are operating on a memory address, first of my_var (line 4), and then of my_ptr (line 5).
Compiling the above program will produce the following output :
We now know that env is a pointer to a pointer which contains a pointer to a function. You may be wondering how we arrived at the last observation, namely that we are dealing with a pointer to a function, and not a function itself. This is due to the fact that functions in C cannot be truly encapsulated within another structure. However, function pointers can.
From a high level standpoint, the env structure has to contain a series of pointers to functions which can handle conversions from "native" code & data structures to their JVM equivalents.
As we saw above, one such function is GetIntArrayElements, which "converts" a JVM int array object to a native int array. The partner cleanup function is ReleaseIntArrayElements, which we will also look at briefly.
Objects in the Land of C
Let's begin by building a few basic structures to simulate the JVM array object and the JNIEnv env struct. An array in Java is basically a C-like array wrapped in an object that adds some basic functionality, such as a variable which tracks the collection size. Our jintArray struct will then be superficially similar to a JVM implementation.
The JNIEnv structure, on the other hand, is a little more involved. What we are recreating is the interface function table the env pointer references. For reference, said table contains pointers to over 200 functions. Piggybacking off the examples laid out in the previous section, we will settle for the implementation of two array manipulation functions : GetIntArrayElements & ReleaseIntArrayElements respectively.
Our interface function table will be a C struct, which are roughly analogous to objects in other higher level languages. As noted earlier, C structs don't actually contain functions. What they can contain are pointers to functions, which we will be defining below.
This is probably also a good time to mention that the order of declaration is important. For instance, we have to declare our structs before being able to use them. Likewise, the main function is located at the tail end of our code. Note that declaration != definition, in that declaration simply means informing the compiler of a variable (or function's) existence, name and data type.
We'll begin with our GetIntArrayElementsFunc function. It is responsible for taking as input a jintArray object and "creating" an array that can be manipulated by native code. As mentioned, a Java array is already a fairly low level construct, so we can create a native counterpart by allocating space and copying the elements of the jvm_array object.
As the refrain often goes, in C a lot responsibility for memory management resides with the developer. This is precisely such an occasion. When instantiating our arrays, we will be allocating memory by invoking malloc, which reserves memory space on the heap.
Of course, we need to include our cleanup function (ReleaseIntArrayElementsFunc). As a rule, any memory that is allocated with malloc will need to be released with free. We can speculate that the purpose of this method will be to free up the memory which we allocated on the heap with our previous function.
You build me up
Once we've defined our functions & structure (templates), we will need to actually instantiate them. In some ways this can be thought of as analogous to creating a new instance of a Class in higher level languages. Of course, structs are not classes, and it's important to keep in mind where the similarities between the two begin and end.
In the case of the jintArray, we populated our mock-object's array (.elements) with 5 ints. Note the use of malloc to allocate memory for our array pointer on the heap. We had previously defined jintArray.elements as being a pointer (to an intarray), and it is for that pointer (.elements) for which we are allocating space.
Thought experiment : in the code provided, we allocate new space on the heap for our "native" array. Is this necessary? After all, the jintArray is an object wrapper around an array. How might you optimize this function?
Our version of the interface function table, aka the env structure, contains references to the two functions which we defined earlier. Note the use of the ampersand, which indicates that we are associating our struct "fields" with the memory location of the functions. By implication, then, our env table now contains function pointers.
Go, JNI, Go
Last but not least, the execution code itself. We will be making all our calls & declarations from within the main function, defined at the end of our program. Notice that lines 11 & 12 are very similar to those we saw in the actual JNI function calls. Some differences being that we use the C int data type and not jint. We also don't include some of the additional parameters passed to the JNI functions (such as isCopy), for simplicity.
You might note that a somewhat hacky workaround was applied in order to get the execution code to mimic precisely that shown at the top of this post. If you recall back to the breakdown of the JNI function call, this all boils down to env not being a pointer to the struct which contains JNI functions, but a pointer to a pointer to a struct.
Beginning with the declaration of env_struct on line 5 we then create a pointer (*env_ptr) which points to the location of env_struct (&env_struct). Then, we create a second pointer with double asterisk notation (indicating a pointer-to-a-pointer), and point it to the location of env_ptr (&env_ptr).
Lastly, on line 12, we call our cleanup function ReleaseIntArrayElements. Our version is essentially a wrapper around free, which de-allocates the memory we originally reserved for the *native_array pointer with malloc in the GetIntArrayElements(Func) function.
I hope this was helpful and, to the extent possible, clear. A lot of the discrete parts of this program could be turned into a discussion unto themselves, and I may build on this in the future. In particular, the concept and application of using pointers-to-pointers is fascinating, and something I intend to explore further.
Source Code & Related Projects :
JNI Reverse Engineering (gist w/ full source code) Matrix Weight Path (Java & C web-api using JNI)
References & Further Reading :
Learn C the Hard Way Calling C Code from Java using JNI C Pointers and memory allocation ##C on Freenode
Checked Exceptions in Java
When learning a new language, I expect a few head-scratching WTF moments. They're really just part and parcel with software development, and coming to terms with them is pretty central to what I'd like to characterize as the Zen of learning.
That said, sometimes they can be hilariously infuriating. It's safe to say that encountering the notion of checked vs. unchecked exceptions in Java was just such an occasion.
Once upon a compile time
Java, unlike most other languages, divides exceptions into two camps - checked and unchecked - and ruthlessly enforces the distinction. The sticking point lies in "checked" exceptions, which (as the name implies) are checked at compile-time, and thus will prevent the application from being compiled unless exception handling is in place.
Take for instance the class below. The code located in the main method will read the contents of a file and print them to STDOUT.
As it turns out, this code will never, ever, run - although it doesn't appear to contain any apparent errors. If the file is located in the appropriate directory, it looks like it should compile and run just fine.
The Java compiler, however, is of a different opinion :
java: unreported exception java.io.IOException; must be caught or declared to be thrown
Checked, not wrecked
The problem of course lies in that Files.readAllLines may return a java.io.IOException. As we can see, the very possibility that a "checked" exception might be thrown is enough to prevent the application from being compiled.
As it turns out, checked exceptions are relatively controversial within the Java community. While there are several ways of handling them, I'm not really in a position to pontificate on best design practices. However, one way of handling checked exceptions, and thus bypassing compile time errors, is with a try-catch block.
In the case above, when an IOException is caught a RuntimeException will be thrown along with a custom message. Another common way of responding to a thrown exception is to print out a stack trace (exception.printStackTrace();). It's included in the sample code above primarily because doing so appears to be relatively common practice, although I personally find it overly verbose most of the time.
Dangerous when swallowed
Another (somewhat dangerous) option involves swallowing the exception. The Java compiler doesn't actually care what happens inside the catch block, only that it exists. Therefore, the following code would also be perfectly valid.
This is understandably fairly bad practice. The exception is swallowed, which is to say that the application will compile and run just fine. The only sign that the above code has failed would be the lack of an expected output.
Hopefully this will be of use to anyone else coming to terms with Java. I'm still wrapping my mind around the language and ecosystem. It's been quite an experience thus far, and I'm certain there are many more fascinating learning moments ahead.
References & Further Reading :
Simple Guide to Checked Exceptions
Checked or Unchecked Exceptions
The Tragedy of Checked Exceptions
Unchecked Exceptions - the Controversy (Oracle)
New Gem : RiDic
Ridic is a Ruby interface for the Regressive Imagery Dictionary, a content analysis coding scheme. It associates words, or word stems, with various concepts and categories such as emotion, sensation and abstract thought.
This really was a case of scratching my own itch. While working on Textalyze and wishing to incorporate RID content analysis as a feature, I noticed there was no Ruby wrapper available for it. There are, however, libraries written for various other languages and frameworks such as Python and Node.
The implementation is fairly straightforward, although a little different from that used by the authors of the Python and Node libraries. They appear to have used the dictionary in its original form, whereas I inverted the structure, creating a Hash with the term as the key.
Original :
PRIMARY NEED ORALITY ABSINTH* (1) ALE (1)
Inverted :
"ABSINTH" => ["ORALITY","NEED","PRIMARY"], "ALE" => ["ORALITY","NEED","PRIMARY"],
Note the asterisk after ABSINTH, which indicates that it is a word stem - whereas ALE is not. That is to say that only ALE, and not all words that *begin* with ALE are to be associated with ORALITY (e.g. not ALEX). Stemmed and non-stemmed words are stored internally in separate structures.
It's also worth noting that the hash is essentially the same structure we might use in a relational table, and we could migrate the data from a hash to a SQL database without much difficulty.
The gem has been uploaded to rubygems.org, and can be installed with the package manager :
gem install ridic
Usage instructions are also located in the GitHub repo and Gem documentation. Two methods are currently exposed :
RiDic.word_match("word") RiDic.stem_match("word")
The first searches for whole word matches, and the second for word stems. I plan to add some additional features shortly, but the above should be enough to play around with the RID. Hope this gem is of use, and feel free to modify and make improvements on it!
Update 12/19/12 :
The API has been expanded, with methods added to return a full list of categories contained in a document, as well as category occurrence.
RiDic.all_categories_in_document("document text") RiDic.category_in_document("document text", category_number) RiDic.category_distribution("document text", category_number)
category_number refers to the "column", or category hierarchy. In the case of "ALE", categories 1, 2 and 3 would be ORALITY, NEED and PRIMARY respectively.
Resources :
RiDic on GitHub RiDic on RubyGems
Web APIs & Net::HTTP
Introduction
Net::HTTP is a Ruby library we can turn to in order to send and receive HTTP requests and responses. To begin with, let's take a look at the API documentation.
Net::HTTP provides a rich library which can be used to build HTTP user-agents.
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.
For our purposes, a user agent means a client application or service that runs on our behalf. We can use Net::HTTP in order to communicate with a web API if by some tragedy a wrapper is not available.
To begin with, we should take note of the fact that Net::HTTP is designed to work with the URI module. In a nutshell, the URI module provides us with a simple way of parsing a URI string.
For example, when fed a URI / URL string, an URI object will expose a number of methods which can, among others, return the host, port, domain and relative path.
Communicating with a Web API
For a practical application of Net::HTTP, we will be interacting with the Saplo API. When browsing the documentation on their page, 3 things become apparent :
The API uses the RPC model. This basically means that we will be communicating exclusively with POST requests which will contain a method call embedded in the body.
There is no Ruby API wrapper or interface
The company is based in Sweden. Not that there's anything wrong with that :)
Our goal will be to get an authentication token from the API using the "auth.accesstoken" method. Let's begin by gathering the necessary libraries.
<![CDATA[// <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]><![CDATA[><![CDATA[> // ]]]]><![CDATA[>]]>
Note that we have also included 'net/https'. The Saplo API makes use of both HTTP and HTTPS, and we will need the latter for the authentication process.
<![CDATA[// <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]><![CDATA[><![CDATA[> // ]]]]><![CDATA[>]]>
Line 4 : Here we have created our URI object
Line 5 : Set the port variable to the HTTPS standard of 443.
Line 6 : We have also created an instance of Net::HTTP - note that only the host and port are required at this stage.
Lines 7 through 10 : These are optional parameters that need to be set for HTTPS requests.
Line 11 : Instantiates a new Post request object, with a header indicating the content type, which in this case is JSON.
Now that our connection parameters are set, we can move ahead by formatting the body and send the request.
<![CDATA[// <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]><![CDATA[><![CDATA[> // ]]]]><![CDATA[>]]>
Line 2 : This is the call to the set_connection_parameters method which we looked at above
Line 3 : Here we set the request body. Note that the entire hash is converted to json. The instance variables @api_key and @secret_key refer to API keys which are declared elsewhere.
Line 4 : We are initiating the request using the "request" method of our Net::HTTP object
Line 5 : The response is parsed in order to retrieve the returned access token
Note that the response is returned as a string, which is subsequently parsed using the JSON module.
Et voilà, we have our authentication token in 8 "simple" steps!
There are shortcut methods available from Net::HTTP, and thankfully most common APIs already have well documented Ruby wrappers. However, I hope the above will be useful should you ever find yourself in the position to have to craft HTTP responses manually.
References & Resources :
Net::HTTP Cheat Sheet
Net::HTTPSession Class Documentation

Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
Free to watch • No registration required • HD streaming
Dynamic Rails Routes
When setting up a Rails application, we may on occasion wish to route a series of dynamic incoming requests. One (somewhat contrived) example might be in the implementation of a music streaming service. As a feature, we might decide to allow for users to search for albums of a given artist by appending the artist name directly to the URL.
Setting aside design and security concerns, our (fictional) URL might look something like this : http://www.arnes-music-emporium.com/the_clash
The following snippet will set up a route in Rails which will catch most incoming requests that do not correspond to a pre-defined route :
<![CDATA[// <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // ]]]]]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]><![CDATA[><![CDATA[> // ]]]]><![CDATA[>]]>
A key first observation here is that the symbol name is incidental. It is simply how we will be identifying the parameter in the params "hash". Thus, an incoming request to /the_clash will route the request to the #search_by_artist action in the artists controller with "the_clash" included in params :
Parameters: {"artist_being_searched"=>"the_clash"}
Note that we are not accounting for a route containing multiple segments, such as if we were to search both by artist, album and song title. Unsurprisingly, we can build a route to accommodate this, and more.
<![CDATA[// <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // ]]]]]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]><![CDATA[><![CDATA[> // ]]]]><![CDATA[>]]>
Here we have added in the ability for a user to provide artist, album and song names directly as part of the URL. The parameters defined inside parentheses are optional. Thus, an incoming request to /the_clash will be routed just as would /the_clash/london_calling/clampdown.
Array Comparison in JavaScript
Sometimes JavaScript feels like a language deserving of its own punchline. Perhaps every time the language trips up some unsuspecting novice, Ashton Kutcher might jump out from behind a curtain, squealing "ECMA'd!".
On the flip side, it's almost refreshing to work with a language that doesn't instantly cater to your every need. Not only does JavaScript lack Ruby's meticulously polished feel, but the object APIs are but a shadow of those that ship with the Ruby Standard Libary.
Take for instance Array comparison. In Ruby, all instances of the Array class come bundled with their own equality comparison method. Let's see what happens when comparing two arrays in JavaScript.
<![CDATA[// <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // ]]]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]><![CDATA[><![CDATA[> // ]]]]><![CDATA[>]]>
Well, that didn't work. What's actually going on here?
As we can see below, it looks like the default comparison will only evaluate to true if the objects are pointing to the same reference (i.e. shallow clones of each other). That's nice, but not particularly useful.
<![CDATA[// <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // ]]]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]><![CDATA[><![CDATA[> // ]]]]><![CDATA[>]]>
It looks like we will have to do this the hard way. Let's visit the logic involved in evaluating the equality of two arrays.
false if the length of array 1 != length of array 2
false if any element at a given index of array 1 != the element at the same index of array 2
true if neither of the above conditions are met
Below is some simple code which implements this, ahem, algorithm. We could do this by simply writing a custom method which takes two arrays as arguments, but instead are adding a function to the Array object's Prototype.
<![CDATA[// <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // <![CDATA[ // ]]]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]]]><![CDATA[><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]]]><![CDATA[><![CDATA[><![CDATA[> // ]]]]]]><![CDATA[><![CDATA[> // ]]]]><![CDATA[>]]>
That's more like it. Admittedly, it would be pretty sweet if we could override the actual equality operator (e.g. ===), however this does not appear to be possible. Alas. For that manner of refined monkey patching, we will have to return to Ruby's warm embrace.