19 October 2010

gem install mysql [--should-be-easy [--on-a-mac-anyway]]

So after upgrading to Ruby 1.9.2 and Rails 3 all at once on my shiny new Snow Leopard macbook, (a foolish thing to do! One step at a time! Isolate errors!), after patching my rails app all over the place till rails_upgrade stops whining, and after a gentle reminder to sudo gem install mysql, I get this:


=> Booting WEBrick
=> Rails 3.0.0 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2010-10-18 23:29:34] INFO  WEBrick 1.3.1
[2010-10-18 23:29:34] INFO  ruby 1.9.2 (2010-08-18) [x86_64-darwin10]
[2010-10-18 23:29:34] INFO  WEBrick::HTTPServer#start: pid=8426 port=3000
dyld: lazy symbol binding failed: Symbol not found: _mysql_init
  Referenced from: /opt/local/lib/ruby1.9/gems/1.9.1/gems/mysql-2.8.1/lib/mysql_api.bundle
  Expected in: flat namespace

dyld: Symbol not found: _mysql_init
  Referenced from: /opt/local/lib/ruby1.9/gems/1.9.1/gems/mysql-2.8.1/lib/mysql_api.bundle
  Expected in: flat namespace

Trace/BPT trap

A search for "lazy symbol binding failed: Symbol not found: _mysql_init" yields this, this, and this ... pointing out that you don't get the wonderful mysql gem without a *little* bit of effort ... but:


$ sudo env ARCHFLAGS="-arch i386" gem install mysql -- --with-mysql-dir=/usr/local/mysql --with-mysql-lib=/usr/local/mysql/lib --with-mysql-include=/usr/local/mysql/include --with-mysql-config=/usr/local/mysql/bin/mysql_config
Building native extensions.  This could take a while...
ERROR:  Error installing mysql:
 ERROR: Failed to build gem native extension.

/opt/local/bin/ruby extconf.rb --with-mysql-dir=/usr/local/mysql --with-mysql-lib=/usr/local/mysql/lib --with-mysql-include=/usr/local/mysql/include --with-mysql-config=/usr/local/mysql/bin/mysql_config
checking for mysql_ssl_set()... *** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Provided configuration options:
 --with-opt-dir
 --with-opt-include
 --without-opt-include=${opt-dir}/include
 --with-opt-lib
 --without-opt-lib=${opt-dir}/lib
 --with-make-prog
 --without-make-prog
 --srcdir=.
 --curdir
 --ruby=/opt/local/bin/ruby
 --with-mysql-config
/opt/local/lib/ruby1.9/1.9.1/mkmf.rb:368:in `try_do': The complier failed to generate an executable file. (RuntimeError)
You have to install development tools first.
 from /opt/local/lib/ruby1.9/1.9.1/mkmf.rb:435:in `try_link0'
 from /opt/local/lib/ruby1.9/1.9.1/mkmf.rb:440:in `try_link'
 from /opt/local/lib/ruby1.9/1.9.1/mkmf.rb:552:in `try_func'
 from /opt/local/lib/ruby1.9/1.9.1/mkmf.rb:797:in `block in have_func'
 from /opt/local/lib/ruby1.9/1.9.1/mkmf.rb:693:in `block in checking_for'
 from /opt/local/lib/ruby1.9/1.9.1/mkmf.rb:280:in `block (2 levels) in postpone'
 from /opt/local/lib/ruby1.9/1.9.1/mkmf.rb:254:in `open'
 from /opt/local/lib/ruby1.9/1.9.1/mkmf.rb:280:in `block in postpone'
 from /opt/local/lib/ruby1.9/1.9.1/mkmf.rb:254:in `open'
 from /opt/local/lib/ruby1.9/1.9.1/mkmf.rb:276:in `postpone'
 from /opt/local/lib/ruby1.9/1.9.1/mkmf.rb:692:in `checking_for'
 from /opt/local/lib/ruby1.9/1.9.1/mkmf.rb:796:in `have_func'
 from extconf.rb:50:in `
' Gem files will remain installed in /opt/local/lib/ruby1.9/gems/1.9.1/gems/mysql-2.8.1 for inspection. Results logged to /opt/local/lib/ruby1.9/gems/1.9.1/gems/mysql-2.8.1/ext/mysql_api/gem_make.out

"You have to install development tools first." ... thanks dudes ... but sudo port instal mysql-devel tells me Error: Port mysql_devel not found. What are these mysterious development tools?

Someone recommends copying the output of mysql_config --cflags to the ARCHFLAGS env var, but still no go:


$ mysql_config
Usage: /usr/local/mysql/bin/mysql_config [OPTIONS]
Options:
        --cflags         [-I/usr/local/mysql/include -Os -arch i386 -fno-common]
        --include        [-I/usr/local/mysql/include]
        --libs           [-L/usr/local/mysql/lib -lmysqlclient -lz -lm]
        --libs_r         [-L/usr/local/mysql/lib -lmysqlclient_r -lz -lm]
        --socket         [/tmp/mysql.sock]
        --port           [3306]
        --version        [5.0.37]
        --libmysqld-libs [-L/usr/local/mysql/lib -lmysqld -lz -lm]

$ sudo env ARCHFLAGS="-Os -arch i386 -fno-common" gem install mysql -- --with-mysql-dir=/usr/local/mysql --with-mysql-lib=/usr/local/mysql/lib --with-mysql-include=/usr/local/mysql/include --with-mysql-config=/usr/local/mysql/bin/mysql_config
all the same errors, all over again

The usual drill, I run these commands a few times to make sure they don't change their minds if I insist politely ... no bacon. But what's the deal with mysql_config5? ... it turns out that it's *not* the same as mysql_config ... it's not the same thing at all, and it has completely different output:


$ mysql_config5
Usage: /opt/local/bin/mysql_config5 [OPTIONS]
Options:
        --cflags         [-I/opt/local/include/mysql5/mysql  -pipe -fPIC    -D_P1003_1B_VISIBLE -DSIGNAL_WITH_VIO_CLOSE -DSIGNALS_DONT_BREAK_READ -DIGNORE_SIGHUP_SIGQUIT  -DDONT_DECLARE_CXA_PURE_VIRTUAL]
        --include        [-I/opt/local/include/mysql5/mysql]
        --libs           [-L/opt/local/lib   -L/opt/local/lib/mysql5/mysql -lmysqlclient -L/opt/local/lib -lz -lm  -L/opt/local/lib/ -lssl -lcrypto]
        --libs_r         [-L/opt/local/lib   -L/opt/local/lib/mysql5/mysql -lmysqlclient_r -L/opt/local/lib -lz -lm   -L/opt/local/lib/ -lssl -lcrypto]
        --plugindir      [/opt/local/lib/mysql5/mysql/plugin]
        --socket         [/opt/local/var/run/mysql5/mysqld.sock]
        --port           [0]
        --version        [5.1.51]
        --libmysqld-libs [-L/opt/local/lib   -L/opt/local/lib/mysql5/mysql -lmysqld -ldl  -L/opt/local/lib -lz -lm     -L/opt/local/lib/ -lssl -lcrypto]

This looks rather serious. Let's plug these values into our gem install command and see what happens:


$ sudo env ARCHFLAGS="-I/opt/local/include/mysql5/mysql  -pipe -fPIC    -D_P1003_1B_VISIBLE -DSIGNAL_WITH_VIO_CLOSE -DSIGNALS_DONT_BREAK_READ -DIGNORE_SIGHUP_SIGQUIT  -DDONT_DECLARE_CXA_PURE_VIRTUAL" gem install mysql -- --with-mysql-dir=/usr/local/mysql --with-mysql-lib=/opt/local/lib/mysql5/mysql --with-mysql-include=/opt/local/include/mysql5/mysql --with-mysql-config=/opt/local/bin/mysql_config5
Building native extensions.  This could take a while...
Successfully installed mysql-2.8.1
1 gem installed
Installing ri documentation for mysql-2.8.1...
Installing RDoc documentation for mysql-2.8.1...

Wow! Bingo! Bring on the beer!

Well there you go I hope this helps, or at least gives you some ideas, if you're stuck with Symbol not found: _mysql_init or extconf.rb failed when you can't get your mysql gem to install.

29 September 2010

Fun with regex: shrinking indentation

Sometimes you get source code from people who believe in 4 spaces. Or 8, imagine! Or tabs ... well that's just *so* 20th century ...


    function($) {
        $("#blah").toto(function(event) {
            $(this).click(function(event2) {
               $(this).goes(WAY.off(2, the("right")));
            });
        });
    };

Too much indentation! 2 is enough! Let's suppose that for whatever reason, you can't or won't use your text editor's re-indent or reformat function, or you just really dig regular expressions ... here's what to do: replace ^( +)\1 with $1.

^( +)\1 means "any nonzero-length sequence of spaces at the start of the line, followed by the same sequence of spaces. The \1 in the pattern, and the $1 in the replacement, are both back-references to the initial sequence of spaces. Result: indentation halved.


  function($) {
    $("#blah").toto(function(event) {
      $(this).click(function(event2) {
        $(this).goes(WAY.off(2, the("right"))); // Not.
      });
    });
  };

There you go. Readable code.

Don't tell anyone I said this, but if you want to do the opposite (increase indentation), replace ^( +) with $1$1 ...

28 September 2010

Stuck for an idea?

If you're a developer with an entrepreneurial bent and don't have access to a real flesh-and-blood business/marketing type, don't worry, these kinds of resources are becoming progressively more and more automated. Here are two to get you started:

The Startup Idea Generator will give you great, buzzword-compliant mission statements such as "Synthesize wireless bandwidth consistently through peer-to-peer AJAX-tagging communities," "Harness robust users in conjunction with crowdsourced android tagging sites," "Implement global infrastructures using venture-backed social media-tracking relationships." You can't possibly lose!

Wait, what does your startup do? will help you hone your elevator pitch and optimize your marketing strategy by leveraging your next-generation wireless browser investments. "So, Basically, it's like a ..."

  • Database Abstraction Layer For The Army!
  • Eco-Friendly Marketplace For Social Outcasts!
  • iPhone App For Ex-Girlfriends!
  • Match.com for Pets!

But don't take my word for it, go see for yourself. It's like an optimizer for your spare time!

Thanks @peignoir for sharing itsthisforthat

15 September 2010

Start In Paris #3

Last night was the third Start In Paris. At La Cantine, as usual, 5 startups presented themselves, so for those of you who couldn't make it, here you are:

Wizme

This is a way of seeking and making recommendations from/to your personal social network. The goal is to make recommendations relevant because they're from people you trust, instead of from random strangers on the internet whose opinions are possibly biased in one direction or another.

The plan for now is to rely on networks you've already established in facebook and twitter. Here's a challenge though: if someone asks you what you think of your new telephone / jeans / dentist over beer, you're generally delighted to expound wisely and wittily on the subject. If you get the same request through facebook, won't it feel a little bit like work? Or am I just old-fashioned?

The concept could be très très cool if it works. No amount of marketing will sell me the latest hot Apple product or hollywood movie if the people I know and respect are telling me it sucks. If this kind of site can successfully undermine the mass marketers, we could be moving to a better kind of world. Of at least, a better kind of France - the site is only in French so far ...

Hop-cube

Hop-cube aims to exploit the growing market of environmentally- and ecologically-aware consumers. Their three services are (1) The "Hop-Badge" that you can show on your site to illustrate how eco-friendly your product is; (2) hop-score.com, which lists zillions of products along with their environmental-impact information; and (3) Special marketing tools based on sustainable development concepts.

Prices and affiliate links to sellers are available immediately on a hop-score.com product page; environmental details require an extra click. Of course they have to make their money somehow, but the way information is prioritised here is surprising given their declared mission. The environmental details page shows the usual stuff - energy use, greenhouse gas emissions, and also information about the manufacturer such as their global gas emissions, water use, ISO14001 certification.

It could be interesting to push this further to understand, for example, how much energy was used to manufacture this product, how much gas emissions, what kind of resources are mined for raw materials for this product, how many rivers are polluted, hectares of forest destroyed for this product, and how many Chinese factory slaves died for this beautiful sleek slender unibody laptop I so enjoy typing on while its mercury-free battery warms my thighs?

On "Sustainable Development Tools", from their site (http://www.hop-cube.com/services/outils-marketing-durables.html, roughly translated), "[our] simulators take into account each of your visitor's specific behaviour and data" ... "besides, the simulators give you a mine of information about your visitors". Well, well. Environment++, maybe; privacy--- ...

In any case, I'll be sure to take a peek the next time I'm buying a great lump of electricity-sucking metal for my home. And delete all my cookies afterwards :)

Dress-Me

Your problem is, you have no sense of style. And you have a major romantic dinner coming up at the end of the week. Or an interview. Or something ... something that causes you, even involuntarily, to think about what you will wear. You have the clothes, even though the few decent ones are musty from disuse. But your sense of style, your finger on the pulse of Parisien fashion, where is it? It's not where you are, and that's your problem.

Dress-Me is a whole platform for whatever you might want to do with your clothes. Buy/sell/exchange/borrow; seek and offer fashion advice; and most useful for someone like me: hire a personal shopper who can expertly tell me what I ought to wear, because, honestly, I haven't a clue.

I can see Dress-Me grow into an awesome community of clothing and fashion geeks, the kind of place where ideas germinate and influence the world outside. Not so sure about popular appeal to jeans-and-tshirt types. But it looks like a lot of fun.

Super Marmite

If you're hungry but can't/won't cook, and you're fed up with the local macjunk, and you simply *know* your neighbours are cooking something delicious, Super-Marmite is where you need to go!

This is a social network for foodies, whatever side of the kitchen you're on. Buy meals, sell meals, meet your neighbours, and eat authentic, genuine, and (hopefully) wholesome food. Especially if your neighbours are Korean, or Japanese, or something like that. Or Senegalese, or Moroccan, or Mexican. Or Italian. You get the idea.

They're planning for two kinds of cook: the casual, occasional kind who's in it for society and fun; and the professional kind, who sees a way to make substantial revenue from their kitchen.

They're still in beta, but I'm sure that won't stop you, go request an invitation

scrumers

If you're unfortunate enough to know what scrum is (hint: nothing to do with rugby), this site deserves a look. One of its features is a "Rich User Interface", and the ability to move tasks around just like post-it notes on a wall!

You might be cynical and argue that you can do that with post-it notes and a wall, but since when did your wall automatically generate burn-down charts, huh? And what do you do when the wall is in another continent? Answer me that you cynical so-and-so.

Comes with obligatory i-phone app. And, unlike every other startup presenting this evening, the folks at scrumers seem to think that somewhere in the world, hiding in a corner on the other side of the planet perhaps, there might be some customers who speak Ze English!! Radical stuff here. Here we are, in the middle of Paris, and the site's entirely in English. Do they think the internet is global or something?

06 September 2010

Discussion

I met a software project manager once who argued that a team of good software developers would be a disaster:

"they would spend the whole time arguing with one another."

Ouch. If they spend whe whole time arguing with one another instead of delivering your project, they're not good software developers. They're crap, in fact. How interesting that this particular manager was unable even to imagine the qualities of a team of good developers, having never experienced one, perhaps!

There's a time to lead, and a time to follow, and an expert (in any profession) knows which time it is. If you find everyone else is talking to much, remember that it might be better to do the wrong thing than to do nothing. It will surely be a learning experience for somebody.

Discuss.

31 August 2010

Mindful Reading

Sometimes, while Bob is reading, a word, a sentence, or an idea on the page will trigger a memory, or a fantasy, or another idea, and quickly Bob's mind is lost in a chain-reaction of these distractions. This might be a minor irritation, but the text-tracking portion of Bob's brain keeps running, on auto-pilot, following words, sentences, paragraphs, even whole pages, while the rest of his brain is away on holiday.

All of a sudden Bob remembers that he was supposed to be reading, it's why he's there after all, and he wanted to do it. But now it takes energy to read backwards and identify the point where his consciousness took the wrong turn, off the page. Then it takes even more energy to re-read the subsequent text, text that his tracker thinks it has already read.

Poor Bob. Lucky for him, he's beginning to practise mindfulness, having been inspired by Mindfulness in Plain English, a short book describing the technique. Mindfulness is the habit of being aware of your immediate environment at all times, including most importantly being aware of what's going on inside your head.

Bob's able to catch himself sooner when a distraction arises, and is able to consciously decide whether to return to the page, or stop reading and pursue the distraction instead (and maybe even write a blog entry about it). Now, he's beginning to notice that he gets more read in less time, and the experience of reading is more satisfactory.

I'll let you know how it goes for him.

27 August 2010

Active Record objects backed by Database Views

"Views" are a powerful feature of relational databases - a view presents the result of a query as if it was a simple table. You can select from a view or join it in another query. You can even use a view in the definition of another view. Using a view, you can hide some nasty, complicated, but useful SQL behind what appears to be a plain old table.

Here's a simple example, using mysql:

CREATE TABLE users (id integer primary key,
                    username varchar(16) not null,
                    crypted_password varchar(128) not null,
                    etc etc);

CREATE TABLE posts (id integer primary key,
                    user_id integer not null,
                    content text);

Suppose you like to keep track of posts-per-user, but you want users.username, not users.id in your results, because you are not the database and you like humanly recognisable names.

SELECT select u.username AS username, 
       (select count(*) from posts p where p.user_id = u.id) as total 
  from users u;

Depending on your specific needs, you might tack on "order by total desc limit 10" or "where u.username = 'me_again_999". It's an awful lot of stuff to type each time. Try this instead:

CREATE VIEW post_count AS
     select u.username AS username, 
            (select count(*) from posts p where p.user_id = u.id) as total
       from users u;

And now:

mysql> select * from post_count where username = 'me_again_999';
+--------------+-------+
| username     | total |
+--------------+-------+
| me_again_999 |    16 |
+--------------+-------+


mysql> select * from post_count order by total desc limit 3;
+--------------+-------+
| username     | total |
+--------------+-------+
| verbose      |   256 |
| eloquacious  |    64 |
| me_again_999 |    16 |
+--------------+-------+

Easier, no?

Now, the cool thing is that this works no matter how ugly, incomprehensible, or complicated your query is. And the über-cool thing is: you can create a Rails model on top of your view, ActiveRecord doesn't care that it's not a table.

We need a view that contains user_id instead of username so that active_record can make the join:

CREATE VIEW `post_count` AS 
      select u.id AS user_id, 
             (select count(*) from posts p where p.user_id = u.id) as total
        from users u;

class PostCount < ActiveRecord::Base
  belongs_to :user
end

class User < ActiveRecord::Base
  has_one :post_count
end

And now:

User.all.each { |u| puts "#{u.username} #{u.post_count.total}"}
verbose 256
eloquacious 64
me_again_999 16

So is that über-cool or what?