#rails

Just updated this blog to Rails 5.0.0.rc1 from 4.2, since this is a pretty small hobby blog app the update was quite simple and smooth.

1) Update your Gemfile, then bundle update rails

# core part of rails 5
gem 'rails', '~> 5.0.0.rc1'
gem 'turbolinks', '~> 5.x'

# use the same web server
gem 'puma', '~> 3.0'

# use latest master branch to make bundle resolve dependencies
gem 'kaminari', github: 'amatsuda/kaminari'
gem 'simple_form', github: 'plataformatec/simple_form'
gem 'rspec-rails', github: 'rspec/rspec-rails', tag: 'v3.5.0.beta3'

# Fix for https://github.com/jnicklas/capybara/issues/1592
gem 'capybara', github: 'jnicklas/capybara'

2) rails app:update to update the configuration files(config/*, bin/*)

Use d to check the diff and y to overwrite if it’s ok.

3) Create app/models/application_record.rb like this

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true
end

4) Update for turbolinks

This is equal to bind on window.onload, DOMContentLoaded, or jQuery ready events

$(document).on "turbolinks:load", ->

Also check the official guide on upgrade your rails app

#ruby #regex

If the task could be achieved by sprinkling some ruby magic, then better do it rather than trying to find the perfect reg expression.

One day I was trying to convert a liquid img tag to markdown style, one tricky part is the image caption can be omitted.

img_1 = "{% img http//example.com/image.jpg %}"
img_2 = "{% img http//example.com/photo.jpg Image Caption %}"

regex = /{% img (\S+) (.*)\s?%}/

img_1.match(regex)
#<MatchData "{% img http//example.com/image.jpg %}" 1:"http//example.com/image.jpg" 2:"">

img_2.match(regex)
#<MatchData "{% img http//example.com/photo.jpg Image Caption %}" 1:"http//example.com/photo.jpg" 2:"Image Caption ">

The caption matched for img_2 contained a trailing space: Image Caption . I was trying to tweak the regex to eliminate the trailing space but my limited knowledge couldn’t get me there.

This is a good place to use gsub with a block, so I could further borrow the String#strip power to get rid of the trailing space.

def convert_liquid_img_tag_to_markdown(text)
  text.gsub(/{% img (\S+) (.*)\s*%}/) do |liquid_img_tag|
    url = Regexp.last_match[1] # same as \1 for inline style
    caption = Regexp.last_match[2].strip

    "![#{caption.to_s.strip}](#{url})"
  end
end

convert_liquid_img_tag_to_markdown(img_1)
# => ![](http//example.com/image.jpg)
convert_liquid_img_tag_to_markdown(img_2)
# => ![Image Caption](http//example.com/photo.jpg)

The link above from thoughtbot mentioned the meaning of having an intention-revealing name, liquid_img_tag in this case, but what’s more powerful is the inside the block it’s all ruby and matched strings, you can do whatever you want with it.

I’m still curious about the right regex to get rid of the trailing space, but for a simple, one-off task it saves a lot of time to ulitize the ruby power inside the gsub block.

Another example is logging the before/after:

def convert_liquid_github_tag(text)
  return unless text

  text.gsub(/{% gist (.+) %}/) do |match|
    html = "<script src=\"https://gist.github.com/kinopyo/#{Regexp.last_match[1]}.js\"></script>"

    puts "#{match} to #{html}"
    html
  end
end
#book

Have you ever felt this way? “This is a great book but I only wish it could be shorter”.

thick book

I’ve never finished Getting Things Done, the famous personal productivity and task management book. Even though I love the idea and concept, there’re way more words seemingly unrelated or off topic. Don’t feel motivated enough to continue, that’s why I dropped out.

Continue reading...

Another one is the 7 Habits of Highly Effective People, part of me still feel ashamed not finishing this book but same reason above, too redundant.

Not All of Us Want Quick-fix But…

It’s not like all of us want a bullet points or checklists to quick fix whatever problems in our life. We pick up the book instead of just googling “how to blah blah blah” right?

So I really hope there is a good balance point once the story is well told, conclusion is laid out, then that’s it, no more off topic contents to make an already great work less great.

Books That Are Short Yet Great

Among all the books I’ve read, REMOTE is outstanding because of its contents and its length. Precise, concise and convincing. I remember the author mentioned that after they wrote the whole book, they cut it to half size to save readers’ time.

The free ebook “SHAVE 10 HOURS OFF YOUR WORKWEEK” by Michael Hyatt is another great example, only 50 pages but it perfectly covers all the points.

And one timely tweet I saw recently is this: “21 minutes edited down to 14 minutes (- 33%) because I respect your time.”

Isn’t this another great aspect of delivering your contents that most book authors are either ignoring or being kept in blindspot? You don’t need to pack tons of words to make it a book, while you can write a blog post.

#adsense #dfp #turbolinks

Turbolinks Compatibility with DoubleClick for Publishers has already provided great solution to let Turbolinks compatible with DoubleClick for Publishers(DFP), but unfortunately just copy and paste the code didn’t work for me, I need to make some modifications to the original code.

Continue reading...

According to the same site, you can no longer use Google Adsense with Turbolinks so you have to use the DFP.

Google has cancelled AJAX support for AdSense, so this solution no longer works. Your best course of action is to use DoubleClick for Publishers.

I’ll provide the whole snippets all involved later, here are the changes you need to make in addition to the code snippet.

  1. Change the markup where you want to display the ads from data-gpt-div-id="div-gpt-ad-123456789-0" to id="div-gpt-ad-123456789-0"
  2. Change the javascript code from divId = $slot.data('gpt-div-id') to divId = $slot.attr('id')

Make sure you disabled your browser AdBlock extension, or whitelist your domain(both local and production) to be able to see the ads. Open browser in incognito mode also works.

Here is all the code for it.

#apple pencil #writing

I’d never had any expectations on “writing with a pen” on an iPad or any electronic device, to be honest. In the past I’ve tried some pens in the market but was never convinced. This Apple Pencil though, “it really could be a game changer”, said by my several friends and multiple sources, and now I truly believe it and its potential.

Continue reading...

iPad Pro 9.7 vs iPad Air 2

After the new 9.7 inch iPad Pro was released last month, I did lots of research on the typical subject: “should I buy it?”

I was struggling between the new Pro and iPad Air 2, the two has exactly the same size, and since I’m not a “power user”: not a designer nor musician, the improved performance wouldn’t help me that much as I often just use this device to read books and net surfing. I almost made my mind to go with Air 2 until I tried the Apple Pencil on my friend’s Pro. The experience was way better than I expected, so I decided to give it a shot, and so far I’m pretty happy about it.

Changes in Writing Style

I’ve written 3 blog posts on the iPad Pro, I’m using the app GoodNotes, which could convert your writings to “e-text”.

It feels much more natural to write with a pen, I mean the Apple Pencil, than staring with a blank screen with a constant blink cursor. And also no need to worry about finding the right link/image or wasting time on trivial editing.

Now my writing flow is

  1. Write first draft on iPad Pro with the Apple Pencil + GoodNotes
  2. Export it to Apple Notes
  3. Edit and publish on laptop

Writing More

I’m writing more. Besides blog posts, I’ve also switched my journals, work notes, daily simple notes and weekly review to this new writing style.

It has the same feeling of writing on a real paper, and also combines the functionalities of the e-notes: accessibility, search ability, portability, rich format.

Indeed the Apple Pencil is really a game changer to me. It’s so good and so intuitive to transfer your thoughts into words. When it comes to writing I don’t think I’ll need the Smart Cover with keyboard.

I’m enjoying this writing style. If you’re interested, try the Apple Pencil first to see if it is the thing for you.

p.s. Here is the screenshot of the draft of this post when it was first written on the iPad Pro, I know my handwriting is terrible :) but at least it motivates me to get things done and dump thoughts into words. 😁

I like to browse various projects on Kickstarter especially those innovative gadgets, and “Back” is the language there meaning “buy to support”. I’ve only backed 2 projects there, bought one solar powered charger for electronic devices like iPhone/iPad, another one is a iPhone bumper case and screen protector. But neither the products nor experience were good, probably even the worst purchase for me in 2015.

Continue reading...

Here are the 3 facts that made it terrible, for me at least.

1. Shipping Problem

The projects on Kickstarter are new. They’re unlike any traditional stuff, the ideas are creative, producers/startups behind it are driven by their visions, the intro video is beautifully crafted, the webpage is full of detailed information, screenshots and product updates, and you’re invited to be part of the big dream, to make it happen they need your support.

But purchasing experience might not reach the expectation.

The shipping of both products(bought from 2 different startups) were massively delayed, massively! They may be good at making the prototype, but when it comes to mass production it could be a totally different story. For both I had received 4 or 5 emails telling the shipping was delayed because there were unexpected difficulties during the mass production process.

2. More Like an Immature Prototype

There’re so many flaws in every details. It’s really hard not to sound like I’m ranting.

For example the solar charger I bought was described it doesn’t require direct sunlight, but that’s simply a false statement. Even in a great sunny day once the direct sunlight goes off the thing just stops working, “It’s still 10 AM, the sky is blue the world is bright and you just won’t work!”, I yelled that day. So far I still don’t have any successful “charge” experience.

Portability is supposed to be a key feature of the product. The intro video demonstrated the usage as: put it on the table when you sit or hang it on your bag when you move, portability™. But what they didn’t tell you the extra work required to “assemble” or “unassembled” it when you change the “mode”. And that’s not like 1, 2, 3 done, it may take minutes..! I’ve seen tons of people asking the same question on the forum and yet the official response was, “basically you just choose one way to use it, we’re not expecting you to switch from one mode to another frequently”. Ouch!

Lastly, it just feels cheap. I know it’s very subjective and may even sound irrational, but remember those experiences when you first touch any Apple products, you feel like you’re holding a great piece of work, you sense the craftsmanship. Comparing things to Apple product probably is unfair for startups but still, it feels like “put this plastic bag on your head to pretend it’s a raincoat”. How can you not be aware of these flaws when you’re making and selling it?

3. The Psychology

I’ve figured out an interesting transition of me after watching intro videos. The producers are not just selling the product itself, but also selling you a dream, a vision, “let’s make it happen together”. They are passionate, young or equally energized, they make a good point why the thing is important to them, to you and to the world, and they are trying to make a good impact to the world.

And here is the interesting part, after I watch all the videos, read all the updates, check all the backers’ comments, I’m already emotionally deeply involved. It’d make me feel proud of myself by “backing” this stuff. It raises my emotional level, it makes me feel good, which is rarely seen in day to day purchase. Self-justification kicks in, it makes me blind about evaluating the real value of the product.


In conclusion, I’m not saying you shouldn’t support or buy from Kickstarter, or persuade you to stop using it. I’m saying you should be more open mind, more tolerant to possible delayed shipping, or some product flaws in some details.

There’re lots of $1 backers, if you do love the idea but after thorough consideration decide to “back off”, it’s always ok to buy them a cup of coffee to show your support.

And those gadgets or stuff in the retail stores or shopping sites, they maybe look less shinny, less innovative than stuff on Kickstarter, but it might be just good enough to be used in your daily life.

Recently I just finished reading the book: David Beckham. It is a great one, but as for the kindle version, it’s a little inconvenient that you can’t take notes or highlights from the book. So I had to go back to “old school”, manual typing :)

So here are the paragraphs I’ve marked, with the page number. Sharing it in case you want to find some great quotes.

Continue reading...


Confidence is a funny thing. People often say that you need a lot of luck to win. But, for me, confidence comes down to preparation. When you have practiced something so much that is has become a part of who you are. Second nature. When you have done everything possible to give yourself the best chance.

(page 24)

I guess when you have practiced like that for twenty years, when you have put yourself on the line for so long, self-belief comes along as a by-product. You know you can do it because you have prepared to do it all your life.

(page 25)

There is something incredible when you strike a football in just the way you want to. It feels so satisfying, the tiny thud of the ball against your boot, and then the fizz of the ball as it speeds away. When you get it right, you hardly feel the impact. It is like kicking a feather.

(page 25)

It’s a strange thing, but throughout my career I have suffered from a particular kind of criticism… It was said that I am more interested in celebrity than application. That I spend more time in front of the mirror than on the training pitch. That clubs only sign me because they can sell replica shirts, not because I can help them to win matches.

(page 38)

If you want to make the best of yourself, if you want to reach your potential, you give it everything. You never stop driving.

(page 38)

My reaction was as quick as it was stupid. I knew he was behind me, walking backwards, so I swung my leg up towards him. My foot probably travelled no more than a couple of feet, but the consequences would reverberate for the next four years. (page 40)

Tony Adams put his arm around me. It was a strong embrace. I could feel that he meant it; that he could see how much I was suffering; that he wanted to take away some of the pain. ‘Look son, everyone makes mistakes,’ he said. ‘Don’t let it get you down. You are going to come back stronger and better.’

I wanted to hold onto him longer, or at least to tell him how much that sturdy embrace and those heartfelt words of wisdom meant to me. But I guess I was too young. It was a lesson in leadership. He wasn’t the captain of the England team at that time, he didn’t have any particular responsibility to look out for the young players. But he was a leader of men. He was a role model.

I have never forgotten those words, or the example he set. Tony Adams is one in a million. I will always appreciate that gesture.

(page 41)

There was one positive to come out of the World Cup incident. One silver lining. One thing that helped me to keep going and to believe that one day I would come through it. It was the reaction of Manchester United. The reaction of the manager, my team-mates and, most of all, the United supporters. They backed me to the hilt. The more the animosity grew, the more they held out against it. The more people screamed abuse at me, the more they chanted my name from the terraces. That was United through and through. That is what the club was about. That is why it is the greatest club in the world.

Alex Ferguson phoned me up. He got straight to the point. ‘Don’t worry son,’ he said. “Things have happened, but it is over now. You are a Manchester United player. We will look after you. Have a few weeks’ holiday and remember that when you get back, you will have the support of everyone. We will protect you.”

That is how things work at United. When things happen outside the club, everything closes around you. Everyone protects you. Nothing gets in, nothing gets out. The manager puts his arm around you even if you are in the wrong. He will never let anyone give any of the United players abuse. He will be the one to tell you to your face, but outside the club he will never criticise any player.

(page 43)

I am often asked: How is it possible to play to your top level when people are baying for your blood? How was it possible to have one of my beset seasons amid all that abuse? Well, the United faithful helped hugely. The were massive and it makes me feel proud to look back on how genuinely supportive they were. The boss and my team-mates helped, too. They were incredible, in the dressing room, on the pitch, everywhere. We had an unbreakable spirit.

(page 44)

It would have been easy to be negative, to worry about the consequences, but I just felt that little bit of steel inside. Partly, it was the extraordinary support I received. But it was also all the practice over the years: the thousands of free-kicks I had taken in rain, sleet and snow. It gave me confidence.

(page 44)

The other thing I always remember was the reaction of Roy Keane. He is a tough guy. He rarely shows his emotions, unless it is to tear strips off you. But he came running over and I could see in his eyes how much that goal meant to him. In some ways, Keane’s reaction has always meant more to me than the goal itself. I have always treasured that.

(page 44)

I also learned that when things go wrong in life - as they always will at some stage, no matter how hard you try - you can’t allow them to defeat you. You have to be strong, to stand tall, to look deep inside and find a willingness to carry on. It is not easy. I don’t pretend that it is. Many people face difficulties that make football seem trivial. But we should never underestimate ourselves. The human spirit is incredibly powerful if you give it a chance.

(page 45)

I guess all this was about more than football; It was, in many ways, an attitude to life. I have always believed that life is about giving it your all. If you do that, if you do something with all your heart, you can look back knowing that you did everything possible to achieve your dreams. It doesn’t mean that you will always get everything you want. It doesn’t mean there will never be hard times. But it does mean that you will reach your potential. And isn’t there something amazing about that thought? And your potential is often far higher than you might think.

(page 76)

Cantona was an incredible player. He was the linchpin of almost everything we did once he made his return. His work-rate in training was phenomenal: he just kept going, practicing the simple things over and over. Then he would get into his tiny, unpretentious car and drive home. It was a revelation to see someone, who looked so naturally gifted, demonstrating a work ethic that took the breath away.

(page 87)

The ball had gone in! The game was back on, the Treble was alive, extra time beckoned. But even as I was trying to take it all in, I could see the manager frantically beckoning us back to our own half. He thought we could win this in the dying seconds. He wanted us to close this out in normal time. How did he know? Was it optimism, or had he seen the opposition sag? One thing is for sure: Ferguson wanted us to press forward. This was the trophy he wanted above all others, the one he had craved since taking the job… He knew that this was a chance to create history, to put another unforgettable memory in the hearts and minds of fans. We knew it, too. But our legs had gone. The entire team was exhausted. It was only adrenaline keeping us going.

(page 107)

It is not often you get a chance to exact the perfect revenge. That normally happens in movies, not in real life. In fact, I never really expected to get one back on Diego Simeone and Argentina after what happened in 1998. But when the chance came, I grabbed it with both hands.

(page 143)

Penalty. England had a penalty.

I walked over to the ball. I could see that Michael hadn’t recovered sufficiently from the tackle to take the spot kick. Every single emotion in my body was telling me that I didn’t just want this; I yearned for it. I could hardly breathe. I know that millions were watching back home, and that a new kind of nightmare might begin if I missed, but I also knew that I couldn’t turn down this chance to create the perfect ending.

Greece had provided redemption. This was an opportunity to write a final paragraph in the story that I never thought I would have a chance to write.

I placed the ball on the spot and looked up. But as I did so I couldn’t quite believe my eyes. Simeone was standing between me and the goal. I almost smiled at the sight of the man whose career had become intertwined with my own, obstructing a penalty that had just been given by the match referee. I know what he was doing. It was as blatant as it was cunning. The stakes were already high, but he was raising them just a little higher.

On the face of things, he was talking to the Argentinean keeper. In reality, he was slowing things down, making me wait, playing games with my mind. It was only ten seconds or so, but it seemed to take an age. Just at the moment the referee started making his way towards him to get him out of the way, Simeone started walking - but not toward the edge of the area to take his place. Instead, he was walking directly at me.

It all seemed to be happening in slow motion. What on earth was he doing, I wondered? Was he going to hurl an insult at me? Was he going to threaten me? Was he going to have a little tug of my hair, like last time? All of this was swirling through my mind as the ball sat on the penalty spot, just waiting to be struck.

I started backing away from the ball but Simeone kept coming towards me. As he got within five yards, I suddenly sensed to my left, Nicky Butt coming towards us. Then, from my right, Paul Scholes started moving. They knew exactly what Simeone was up to. They knew that he was trying to get inside my head, to plant a seed of anxiety before this crucial moment. And they weren’t having any of it.

It was a massive moment of reassurance. The pressure was intense but I know that I had my team–mates with me. My friends. The guys I had grown up with and who how had my back. All those feelings of togetherness, that sense of the unbreakable spirit at United, seem to come back to me. I was already feeling good about the penalty, but now I oozed confidence. I had no doubts as I stepped forward.

Simeone’s antics had vanished from my mind.

There was another slight delay before I began my run–up. I looked at Collina for the signal to take the kick, then fixed my eyes on the ball. I took a few deep breaths. Finally, Collina blew the whistle and I instantly run in. I hit the ball as hard as I could towards the middle of the net.

It may not have been the most elegant penalty ever taken, but I instantly knew it was in, and I continued my euphoric run, beyond the area towards the side of the pitch. I felt a torrent of emotions rushing through my body and mind. I had done it. I had scored against Argentina in the World Cup.

It was not just about revenge, although that was a massive part of it.

For me the 2002 World Cup will always be about that defining moment against Argentina. It was a penalty, but it was also the perfect ending to an incredible, and very personal, story.

(page 147 - page 149)

The only problem I faced from the fans in my time at the club(Real Madrid) was in a match against Barcelona. We were 2-0 down and the crowd was getting tetchy. The Real-Barcelona rivalry is as intense as anything in world football and the team from Catalonia were in control. As I ran towards the touchline, an older guy stood up and gave me an earful. I looked at him for a moment before turning back to the game.

It wasn’t a terrible thing he shouted, particularly when compared with the abuse I had to put up with from away fans in the 1998-99 season, but for some reason it got into my mind. Over the remaining minutes, we got our act together and fired back into contention. Eventually, against all the odds, and with the fans going crazy, we turned the deficit into a victory. It was one of the most brilliant comebacks in all my time at Real.

Afterwords, I ran over to the fan, whose face I had remembered, smiled at him, and then lifted my shirt over my shoulders and handed it to him. He beamed back and gave me a hug. Somehow it felt amazingly good. We had won the match, but I had also won over a detractor. That is always the best way to get someone to change their opinion of you: to work hard and prove the wrong, rather than just getting irritated and shouting back. It was a powerful moment, too, because many other fans were touched by the embrace.

(page 182)

“We knew about David’s qualities with the dead ball and his ability to cross well,” Zidane said. “But it is impossible to appreciate how hard he works for the team and how much of an unwillingness to lose he transmits until you play with him.”

(page 184)

I put everything into my training and matches. I knew there were lovely beaches and tons of opportunities to have fun, but I didn’t want to do anything to compromise my preparation for games. My focus never wavered. If I was going to change the way people thought about American soccer, I had to be professional in everything I did on the pitch, and off it. I had to set an example.

(page 210)

I always knew my final match would be emotional: The game has always meant so much to me: it is more than a passion, more like an obsession.

(page 257)

I play the game because I love it: love the competition, love the friendships, love the feeling when I get a chance to strike a corner or free-kick… Playing the game is a part of my identity. It is in my soul and always will be.

(page 258)

How could anything replace football? The game is like nothing else and anyone who hopes that they will be able to recreate the feelings and emotions of the game after they have retired is kidding himself.

(page 258)

Now, as I look forward to the next chapter, I am content, even as I am filled with sadness. Content with what I have achieved, content with what I have learned, content that I gave it everything. I left nothing out there on the pitch. But then, I guess, that is one thing that will always be true. It is almost a personal motto, something that I try to pass on to my own kids.

Whatever you do in life, give it everything you’ve got, with a smile on your face.

(page 258)