Category: Uncategorized

Rails: I can put together a simple database-backed app really quickly

Comment left here

… and that’s why I use RoR. 90% of the “anti” opinions here don’t get it. It’s quick, it has a low entry barrier, and, if you want the Ajax stuff, it’s very easy to do simple things. If you want stuff that scales to the moon then you use a technology that will do that – d’oh!

Ruby has also got lots of nice utilities to do SOAP and so on. It just works and it does’t hurt. Integration with other services (.Net or Java) is a breeze and you can get help really easily from other people in the community. In general there is only one way to do something (because the language is young) and it does what you want. Compare with Java – 25 frameworks with alpine learning curves (XML or properties files – let’s have both!) before you can do anything. It used to be simple and pretty easy to get things done but the signal to noise ratio is really painful now and getting worse with every “improved” J2EE implementation from the big vendors. Me no want no stinki’ entity beans – they leave a stain on your teeth.

I am a Java certified web developer and would’t go back, except for the cash. It used to drive me crazy: make a change, run Ant, deploy the WAR file, waste 20 minutes doing nothing. Get shouted at by PHB for reading El Reg while all this was going on. This is typical if you work on a legacy system.

Now I can just get stuff done, and that’s all I want. I’m really keen on the whole JRuby thing, where the Rails app will just run from a WAR file and use all the sexy scalability stuff that the Java people have spent so much time and energy on – but I’ll happily use it at one remove, thanks.

Getting the number of months between two dates in Ruby/Rails – updated

Here lies the one that works, not the old one I posted ages ago. It also uses dates instead of timestamp differences and should therefore not break if you ask it to go before the Epoch (some time in 1970 I think):

Adrie’s comment left on my old blog pointed this out (he’s much cleverer than me!) – it’s really trivial:

s is the start date and e is the end date (s is lower than e)

(e.month - s.month) + 12 * (e.year - s.year)

See!

module DateUtils

  class << self
    def months_between(date1 = Time.now,date2 = Time.now)

      date1 ||= Time.now
      date2 ||= Time.now

      if date1 < date2

        recent_date = date1.to_date
        past_date = date2.to_date

      else

        recent_date = date2.to_date
        past_date = date1.to_date

      end

      (past_date.month - recent_date.month) + 12 * (recent_date.year - past_date.year)

    end

  end

end

Imported Comments:

UE

Thanks a lot. helped me much

john

not working

fdsa

doesnt this neglect the actual day of the month? there isn’t 1 month between 2/1/11 and 1/31/11, but this would claim there is…

Francis

ok – but “months or part therof” is a long method name 🙂

Email sent in response to the vision thing geekup email

Start here to see what this is i response to

… how many of you go back to the pre-web days? I do – graduated in 1987, I’ve had to practically start again 3 or 4 times in the last 20 years – Unix before Linux, X-windows, dumb terminal to client-server, pre-javascript web tech, javascript, hand crafting your own stuff in raw html, Java, PHP, Java again, Rails. All I’ve ever wanted to do was deliver systems that work and meet real-world problems in a usable way. Be able to go home at the end of the day feeling I’ve achieved something and the people who use the stuff I wrote can do their jobs more effectively and not be dehumanised by it, hence my dislike for techie arrogance, which I see all the time.

2.0 is a Tim O’Reilly marketing thing, because he saw a lot of problems with the current technology and wanted to have some kind of dividing line to help people distinguish between things. But like the man at ucov says: no business model, no idea what people want, groovy idea that recycles some stuff other people have already failed at = failure. I’m with Paul Graham on this one – work on something boring and painful (for the punters) and you will see the rewards. Shiny is’t necessarily good1. You seem to have bought the marketing hype.

I applaud people’s enthusiasm – I want technology to work and help us get out of the social and cultural ruts we’re beginning to die from (a rut is a grave that has’t had the ends filled in yet). Debate is healthy and really useful. The best “manifesto” is something people find useful and the culture that coheres around it. I’d rather work on that than an essay, any day.

F

1 for some reason line from “venus in furs” started going through my head when I wrote that, must get new prescription for meds.

Phorm are those 121 timewasters? Great! Now I know who to send the bill to!

Comment left here  

Did’t realise that phorm were the timewasting bastards who were behind 121 – spent many a happy hour trying to get rid of their viral nonsense from a machine my then 10 year old son was using (no idea how they got past him not being an administrator).

Can I send them a bill? I think they also managed to hijack firefox a while ago by putting in a bogus (and invisible) add on so I had to trash everybody’s settings directory to get rid of it.

DEFINITELY send them a bill, and then a summons through the county court for my time. Anyone else want to join in?

The Met have gone mad – glad I live in Merseyside

Comment left here 

Bloody hell! Although I suppose it might save us from tourists (joke, definitely joke, I hope). Is there any forum you can have a go at them about wasting public money? There’s an idea – why not get off your backsides and do your job preventing it? Maybe pay for some police officers with the cash you just threw down the bog?

It’s like the “ID cards stop terrorism” argument – how exactly? Unless you are going to start something like the pass laws like they had in South Africa under apartheid? And we all know who will have their movements restricted, don’t we? All of the guys on 7/7 were UK citizens and would have had valid ID. No-one suspected anything or they would have done something about it. Gah!

The authorities have to be seen to be doing something so they spend money on paranoid crap like this.

DHH the windows hater – or maybe needs to think a bit before he spouts off?

Comment left here  

I am a Rails developer who uses a proper IDE (Netbeans for Ruby) and Windows. Installed Cygwin to give me the bash shell and the other tools I need to talk to our Linux server backbones. I started on Unix/Sun workstations, went to client/server stuff then on to web. DHH is a kid, bless him, and has’t yet lived through the next big change that will make him realise he has to start again. I’ve done it maybe 5 times in the last 20 years. Experience of one’s own ignorance makes one humble. He’s on a high now. Losing your job is a great and unwelcome teacher, believe me.

I don’t get the Mac thing, either. If you don’t want windows Ubuntu is fine, has the sexy desktop thing and runs on more powerful kit that costs a quarter of the price.

Don’t get me started about TextMate – it’s a word processor with a bunch of arcane key combinations to run a pile of bundled bash shell scripts. I could probably do something similar with Vim or Emacs but don’t need to because the nice guys from Sun have written very good free tool. Can’t split a view of the same file – just like a word processor all you can see is the few lines around where you are working. http://francis.blog-city.com/debugging_rails_applications.htm

Java is bad for your brain talk at Barcamp Manchester

Following on from the talk I gave on why Java is bad for your brain at Barcamp I thought I’d dig out some of my old blog posts and present them here – try and explain the argument (which is more of a feeling of disquiet tha anything else) better.

download mind map here (save as)

I tried to embed the map in an applet – but the downloaded applet does’t work and locks the site up. There is a Flash reader but I lost the will to live Java, eh?

You ca get freemind here. 

Defensive programming

http://francis.blog-city.com/what_defensive_programming_is_and_isnt_logging_the_right_t.htm

This one’s about ‘C’:

http://francis.blog-city.com/at_the_feet_of_the_master.htm

Java is the new ‘C’ parts 1 & 2 

http://francis.blog-city.com/at_the_feet_of_the_master_1.htm

http://francis.blog-city.com/at_the_feet_of_the_master_1htm.htm 

J2EE is incoherent

http://francis.blog-city.com/cohesive_libraries.htm 

Java schools

http://francis.blog-city.com/java_schools.htm 

Java as a text processing language – note the comment about merely trying to ope a file

http://francis.blog-city.com/java_as_a_text_processing_language.htm 

Java and XML

http://francis.blog-city.com/java_finding_nodes_with_xpath_and_how_to_dump_out_xml_dom_as.htm 

SOAP calls make activerecord stop working, oh my

Create an active record object, retrieve some stuff from a soap service and then get this:

NoMethodError: You have a nil object when you did’t expect it!
The error occurred while evaluating nil.has_key?
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/base.rb:2172:in `has_attribute?’
 

If you’re having trouble with active record suddenly throwing exceptions it’s probably because of a namespace collision with your Soap classes defined in the generated file called default.rb, or YourClassName.rb depending upon what arguments you gave to the wsdl2ruby command.

You need to put the data transfer classes into their own module by adding

—module_path MySoap

to the end of the command line. So you have something like this:

wsdl2ruby.rb —wsdl my.wsdl —type client —classdef My —module_path MySoap 

(remember to delete the old files!) 

I last fixed this by hand editing the files and adding in the module definition – so this is by far the easier way.  

Another problem is you get this:

NameError: uninitialized constant SOAP::Mapping::EncodedRegistry 

Just put

gem ‘soap4r’

in the mapping registry file 

SQL is dead?

Comment left here. 

XML – sigh

I read Oracle’s early papers on XML DB – “we store the schema and the data separately, with pointers into the data”

Codasyl in disguise. The same with the o-o databases, hierarchical databases – none of this is new at all.

Personally I can write SQL, I understand the syntax and have been using it for over 20 years. Once you realise you’re really working with sets it’s pretty easy to understand.

RDBMS is now just another commodity. I’m happy to use whatever comes next, I currently develop in Ruby on Rails / MySQL and hardly ever have to see the database directly. Interestingly, the people who designed Active Record made 90% stuff really easy and then allow you to drop into raw SQL for the complex, instead of retrofitting the kitchen sink.

I’ll use whatever comes down the pipe, as long as it meets my needs – that’s it.

Deep merge a Ruby hash, the joys of recursion

I was doing some hacking with lists of valid phone number codes (3,4,5 digits depending upon various factors). I used hashes that could be walked down because of the key lengths. Now we’ve decided to do it  differently because the data file was’t up to date …

This gets the phone number as an array of integers:

      code_pieces = code.to_s.split(//).collect(&:to_i) rescue []

(note the Rails &: thing)

This takes a list of keys and a value, pops the value off the beginning of the array and when the array is done then puts the value in with a hash key of -1:

    def hash_for_digits(n_array,value)
      n = n_array.delete_at(0)
      if n
        the_hash = {}
        the_hash.merge({ n => hash_for_digits(n_array,value) })
      else
        { -1 => value}
      end
    end

This is a merge of a hash of hashes with another – warning – it blows up  without the -1 markers from the previous function.

    def deep_merge_hash(hash1,hash2)
       hash2.each_key do |k1|
        if hash1.key?(k1)
          deep_merge_hash(hash1[k1],hash2[k1])
        else
          hash1[k1] = hash2[k1]
        end
      end
    end
 

Hope others find this some use – I might return to this another day and make it work with arbitrary hashes but now too busy and did’t want to throw the code away … 

The return from the function looked like this:

      top_number = get_code_ref[code_pieces0][code_pieces1][code_pieces2] rescue nil
      if top_number
        ok = top_number[code_pieces3][code_pieces4][-1] rescue nil
        ok = top_number[code_pieces3][-1] unless ok rescue nil
        ok = top_number[-1] unless ok rescue nil
      end
      ok

I realised I could probably have speeded things up by having the first 3 as a hash key in their own right but too late now – reimplementation with a different data set on the way …