Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Monday, January 30, 2012

Learning about closures in ruby

Per Wikipedia, a closure is defined as: "In computer science, a closure (also lexical closurefunction closurefunction value or functional value) is afunction together with a referencing environment for the non-local variables of that function.[1] A closure allows a function to access variables outside its typical scope."

Taken on just its pure definition, it's a little difficult to conceptualize, especially for someone like me who comes from a mostly iterative programming background. So let's look at some sample code (I modified some sample code about Ruby from Wikipedia)



puts "learning about closures"
def create_closure(initial_value = 0)
  closure_value = initial_value
  return Proc.new do |x|
    closure_value = x
  end, Proc.new do
    closure_value += 1
  end, Proc.new do
    closure_value
  end
end
one, two, three = create_closure
myval = 21
puts "one.call(myval) == #{one.call(myval)}"
puts "two.call == #{two.call}"
puts "two.call == #{two.call}"
puts "three.call == #{three.call}"


This outputs:

learning about closures
one.call(myval) == 21
two.call == 22
two.call == 23
three.call == 23




Each proc here is a closure, because the scope of each proc is bound to the scope of where it was defined. In this case, all of the procs are bound to the scope of the create_closure function.

So you can see that even though the execution of create_closure is finished, each proc still has access to the same local closure_value variable that was defined within create_closure function.


Sunday, October 24, 2010

i18n bug in Rails




I just ran into a bug when we installed a new gem in the same Ruby install that runs both our test environment and our Redmine install. The problem is that the new i18n gem is not compatible with rails 2.3.5.

The solution was to change this line
gem 'i18n', '>= 0.1.3'

to this
gem '18n', '<=0.4.1'

To ensure that Redmine grabs an earlier version of the i18n gem. Hopefully this saves someone else out there from a few hours of frantic googling.

Saturday, July 24, 2010

Repository browsing with redmine

Redmine plays well with Git. It was so easy to set up, everyone who uses redmine and git should do it.


Using Redmine is now my default way to browse repository commits.

Tuesday, July 13, 2010

f.lux is saving my eyes

Like many technology workers, I suffer from overuse injuries of the fingers and wrists, as well as constant eyestrain. I do much of my programming at night with the lights off, and while OSX has good built in functionality for dimming a monitor (or using the Shades app for secondary monitors), it didn't seem like enough.

I could either have the brightness turned up so I can see, but have my retinas blasted with excess photons, or have the brightness turned down, and then squint as I try to read.

Enter f.lux.

Instead of messing with the brightness of your monitor, f.lux changes the colors of your monitor to be easier on the eyes. At first, it was kind of an uneasy yellow color, but my eyes adapted quickly.

Everything felt normal, until I turned f.lux off. My monitors colors returned to normal, and I instantly felt like icepicks were being driven into my eyes. Needless to say, f.lux was turned back on, and I use it every night. I heart f.lux.

Thursday, May 13, 2010

Git

Git is so much easier to work with than subversion, it makes me wish we had switched sooner.

Tuesday, April 6, 2010

More on simple databases

When I think about it some more, the difference between something like MS SQL Server or DB2 vs Hibernate + MySQL with MyISAM, is that the traditional way is for the RDBMS to be a single system, relational management AND static data storage. The persistence layer/db strategy is separating the RMS (relational management system) from the DB (static storage). By separating out the relational management portion, and moving that into the persistence layer, you have the ability to run one processing cluster which serves your application and handles the persistence layer/rms, as opposed to running two clusters, one to serve your application, and one to handle the traditional RDBMS. I would think that combining your servers into one larger cluster to handle more of the computing would be more efficient than splitting it into two clusters, in which case one cluster may hit max load while the other sits idle.

By the way, I know that there are measurable differences between MyISAM, InnoDB, or other storage engines, like row level locking as opposed to table locking on updates.

How I'm learning to love simple databases

Once upon a time I was a total believer in strict database rules. Everything should be fully normalized, schema and diagrams thought out ahead of time, strict data validation at the database level, so on and so forth. A total believer in the primacy of MS SQL Server, DB2, so on and so forth. Within the last 6 months, I've had a change of mind that's been brewing, and only lately have I more fully realized it.

When you're developing a webapp nowadays, chances are you have various frameworks and technologies that you use, each one layered on top of the other. MySQL -> Hibernate -> Java application -> JSP/Servlets -> HTML/markup, as one example. The data that you accept in from your users must pass through each layer to finally get to your storage, whether it's in memory at the application layer, or static storage at the db. Each layer has its own data validation. So if you have a date field that a user fills out, it comes in as a string, which the Servlet can accept in and convert to a valid Java date, which passes it to your application layer, in which you can put that data in an object that checks that it's a valid date (if it's defined as a Date object). Then it goes to your persistence layer, which in our example, Hibernate would check that it's a valid date. It finally gets passed to your database layer, which in our example, MySQL would check that it's a valid date. All that work to verify that it's a date several times. Why? What's the point? If you checked that it's a valid date once, and you don't do any measurable transformation of the data, there is no need to check it again.

If I think about it, when is it exactly, that I care that I check if it's a valid date? When I accept data in from the user, when I do fancy things at the application layer like date/time arithmetic, and when I display data back to the user. None of that touches the database layer. Do I care if the static data is stored in a traditional RDBMS, a bunch of XML files, or a straight up B tree like GT.M? Other than the possible performance gains by storing static data one way or another, it doesn't matter.

I currently use Wicket, and Wicket has easy to use form validation built into the framework itself, which covers all the cases in which I would care to check if the data is appropriate or not. Right now, I do datatype management at each layer because that's how I was taught, but I'm seriously considering switching everything underneath Wicket to string, integer, or float, crossing my fingers, and hoping nothing blows up.

Beyond data validation, what about primary/foreign keys? Many databases don't even have pk/fk relationships built in. Originally, I thought those databases were mad (in a bad way). However, if you think about it, the business logic which says that one table must relate to another table via a field based relationship, that business logic has to exist in your RDBMS, your persistence layer, and your application (because you must pass data with the appropriate relationships to the layer below). Why should each layer check the relational integrity of the data? The only case I can think of, is to protect myself from making a mistake at the application layer. Fine then, I can set those rules in the persistence layer as well as my application, and the database can be simply a static container, nothing more.

Wednesday, July 22, 2009

I Heart Automator

Every time I wish there were some way I can automate something in OSX, I find out there is a way, through the nifty scripting utility Automator.

I had a particular problem I wanted to solve, which was to add spotlight comments to multiple items. This way I can do dynamic searching of files based on keywords instead of creating an entire folder hierarchy and having to drill down to get to a specified file.


And here's my Automator plugin in action, accessible right from the finder menu. Slick!



Saturday, March 7, 2009

Automator for OSX

I've begun using a software VPN utility called Hamachi, owned by the guys at logmein (every family IT dude's friend). Alas, there is a slick Windows client, but the OSX one is pure command line. I'm totally fine with that, but I figure "let's see if I can make it all OSeXy". So I created a bash script to execute all the Hamachi commands, and I combined that with some Applescript and OSX's Automator, which is kind of a wysiwig script editor. I could have done it all in bash or Applescript, but I wanted to put my script on the dock and run it from there. Only Automator "executables" can be moved directly to the dock.

Here's a sample from my automator window. Note how cool it looks to have a wysiwig script editor!


So the end result is I have a clickable "thing" on my dock which will start up or quit the OSX Hamachi client. I also added in a Growl notification for fun, which is shown below.

Sunday, August 31, 2008

Rad Studio crashing again!

It's damn frustrating, this is twice in the last 2 hours. I'm not even doing anything weird, just editing a source file. I checked the ram usage of my workstation, it's at 600mb out of 4gb total. Rad Studio crashes more than any IDE I have ever used in my entire life. 

Tuesday, July 29, 2008

A new age of military healthcare?


So to take a break from all my Delphi stuff, I figure I'll post about where I've spent most of my career, which is in healthcare. The majority of my experience comes in working with the VA's EHR, named VistA. VistA by the way, was created at least a decade before Microsoft Vista. Recently, I've become more interested in CHCS and AHTLA, which is part of the DoD EHR system. Interestingly, it seems that MHS (Military Health Systems) is turning to new web based technologies to gather feedback from their users. I'm glad to see some of the MHS leaders taking advantage of social technologies.

Wednesday, July 23, 2008

Darwin Race of Langauges is Over!


Congratulations to all who finished! My downfall was I bit off more than I could chew by using a semi-familiar IDE and language, while attempting to learn COM and WMI. No excuses though! Even though I couldn't finish the race, I had fun and I learned more about RAD Studio in the process.

My one regret is that due to all the problems to solve being GUI based, I couldn't enter using my best language, GT.M. I bet that would be something no one else has seen =P.

In any case, that's just me rambling on (and trying to find some way to work GT.M into my post). Too bad none of the people who finished the competition entered anything for OSX.

Sunday, July 20, 2008

Special kind of error

I tried to cast an internal control to TwwDBGrid as a TwwCheckBox and use it's Checked property. All I can say is WOW!

Here's the follow on error

That is a special error. As in "Special Education".

Whoops, I still have more time!

For some odd reason, I thought that the competition ended on the 18th. It turns out it actually ends on the 22nd, so I still have more time to work on it. The next block of free time that I have will be on the 21st, so we'll see if I can work some magic to figure out WMI and COM before the actual deadline.

Friday, July 18, 2008

Darwin Race of Languages - Day 4


So I've been defeated by COM and WMI. To me, what's missing in the Delphi world are comprehensive tutorials and documentation like you can find with MSDN and Sun (for Java). What I ended up trying to do was look at existing code that uses WMI in Delphi, and attempt to reverse engineer it. I have a reasonable understanding of the objects and techniques used in the sample code, but when I got stuck, I had no idea what was causing the problem. Unfortunately, the time ran out for the competition before I could make anymore headway. 

Earlier in the competition, I posted about my usual steps when learning something new, for the Darwin Race of Languages, I got stuck on step 2, developing the proof of concept, by reverse engineering existing examples.

If anyone is interested in looking at the code I was working on, the entire project is in a zip file here. Note that I'm not sure if all the imports will be correct on anyone elses pc, as the paths were local to my pc. I can't really call the code "my code", since it was mostly a cut and paste from someone named ciuly. I also used as reference, this project from Planet Source Code, which interestingly enough had errors, but ran on my workstation. When I attempted to extract the code and place it in my project, I ran into the same EOleException that I had in ciuly's example. That leads me to believe that the issue lies somewhere in my project, but not in the sourcecode. 

Darwin Race of Languages - Day 3 Part 2

Still no luck. All I can find are MSDN articles related to WMI on Microsoft's official languages, such as C++, C#, VB, etc. I can also find many examples of using WMI in Delphi, but there are no comprensive, newbie friendly tutorials on the different WMI components as related to Delphi and RAD Studio. I'm sure it's a simple fix to solve my problem, but those simple problems are the things that a newbie friendly tutorial will help walk me through.

When I was learning C#/.NET, I did most of my learning on MSDN. I've done multithreading, socket connections, registry modification, file encryption, used active directory, so on and so forth. I never once had to ask the wider .NET community for assistance, as MSDN has always been good to me. Ditto for learning Java, Sun's documentation is good as well (at least it was back when I was doing Java, the version 1 days).

uses
  Google, MSDN;

implementation
procedure Will_I_Win;
var
  Delphi: New_Language;
   RAD_Studio: New_IDE;
  WMI: New_Component;
   Success: string;
begin
   RAD_Studio := IDE.Create('CodeGear');
   Delphi  := RAD_Studio.Language;
   WMI := Component.Create('MS');
   success := 'You Fail';
   while OtherProjectsGoingOn do
   begin
     if Solveable(Delphi, RAD_Studio, WMI, "2 weeks") then
     begin
         success := 'yay!';
     end;
   end;
ShowMessage(success);
end;



Darwin Race of Languages - Day 3 Part 1

I've been reading through tons of examples of using WMI with Delphi, and I've tried running what seems like the most likely candidate for what I want to do (return the CPU temperature). However, I keep getting the error below.


It seems like I am unable to use Sinkclass.DefaultInterface when I call Services.ExecNotificationQueryAsync. Since the "Sink" is supposed to be an eventhandler that fires upon receipt of an event from Services, I guess this is a good error, since I don't have any code defined to handle the event, and I have absolutely no clue where DefaultInterface is in my Sink object. 

I'm not sure if I will be able to complete the project before the deadline, but I'll keep trying until the time runs out.

Sunday, July 13, 2008

TStringList's has poor parsing capabilities

TStringList cannot handle spaces in its DelimitedText property. So the workaround is to ensure that your strings with spaces are enclosed in chars that are defined in the TStringList QuoteChar property. So what happens when you use the nifty LoadFromFile procedure on a large delimited text file with many spaces but no QuoteChars? 

The answer is you get a messed up TStringList, because if you have no QuoteChars, TStringList assumes that spaces are delimiters too. Isn't that fun!

The parsing capabilities of TStringList are half-assed at best. In addition to the spaces/QuoteChars fiasco, your delimiter can only be a string with a length of one.

And don't worry, I've already programmed around TStringList's shortcomings. I had to load the file and parse it myself, adding quotes where necessary, then passing it to TStringList.DelimitedText. 

Friday, July 11, 2008

My Delphi Race of Languages day 1

I've checked out the progress of some of the other competitors, seems like my late start has put me behind by a few days. That's alright though. My usual approach to a problem that I don't understand, is to do the following:
  1. Use my google-fu to do some research
  2. Get a proof of concept working
  3. Design the actual application
  4. Testing and refinement
So I'm at the google-fu stage. So far, it seems like the way to go in Delphi is to use WMI (Windows Management Interface), which is a part of the Win32 API, but I need to set up my project in RAD Studio to be able to use it properly.

I entered the Darwin Race of Languages


So my rantings on Delphi and RAD Studio sort of spawned a programming/IDE competition hosted here. I got an invite to compete, and I figure... why not? The worst that could happen is I become better at RAD Studio and Delphi, which is a good thing. I'm sure the critics that I have would love to tear apart my code, and I'm going to give them that chance!