Life of Larry

A One and a Two

Share My Picks II

| Comments

Recently, my colleagues are killing it at sharing picks in our daily stand up meetings. I really learnt a lot from everyone else’s picks. So it’s highly recommended to do daily sharing in your team too!

I select some items from our picks to share with you folks, here is the list.

Tools

  • Git Radar: Git-radar is a tool you can add to your prompt to provide at-a-glance information on your git repo.
  • Quip: Quip is the modern productivity suite that simplifies your life and helps your team get work done faster. I use quip recently to write some design documents and share with my friends. And the experience is really good, it fits well my needs for document writing and collaboration with friends/coworkers.
  • RequestBin: RequestBin gives you a URL that will collect requests made to it and let you inspect them in a human-friendly way.
  • httpbin: Testing an HTTP Library can become difficult sometimes. RequestBin is fantastic for testing POST requests, but doesn’t let you control the response. This exists to cover all kinds of HTTP scenarios. Additional endpoints are being considered.

Articles

See you next time.

How Can I Survive in the Bay Area?

| Comments

Just read a topic titled “How can I survive in the Bay Area?” on Quora, and the question is like this:

Both me and my husband work at top notch companies in bay area and make around 400k. Even with this kind of salary we are unable to buy a single family home in Cupertino. We want Cupertino because of good schools and shorter commute to our work. What should we do in addition to our regular job to be able to afford such expensive houses? Any suggestions on investments/ business etc ?

In short, this couple makes around 400k dollars a year and is unable to live a life like they want. They are wondering what they could do about that?

I’m not saying I agree with the top answer one hundred percent, but it somehow reflects the current situation of life in Silicon Valley. I won’t know whether we could survive that until I could get selected in H1B lottery next year. Actually, on second thought, this answer seems irrelevant with the city, no matter it is Silicon Valley, New York, or Beijing (where I live for now), it fits all big cities with similar problems somehow.

So here is the top answer for the topic. It’s a little long but very worth reading.

Source: http://www.quora.com/How-can-I-survive-in-the-Bay-Area/answer/Michael-O-Church

The brutal, complete answer: don’t procreate. Sure, you may be the smart, thoughtful sort of person that humanity absolutely needs in its gene pool, but Silicon Valley doesn’t want you in its gene pool: you don’t make enough to own a house in a top school district. It’s sending you a clear economic signal. So, you have two options.

Share My Picks I

| Comments

In the end of every episode of Ruby Rogues, which is a famous podcast focusing on Ruby/Rails development I really like, everyone will share their picks. A pick could be a tech book, a fiction, a blog article, an interesting library or anything worth taking a look.

In our team’s everyday stand up meetings recently, we try to share our picks to other team members what we found interesting. And I really enjoy doing this, since I love sharing to people. Before the Google Reader shut down, my friends and I shared lots of articles on it and it benefited me a lot. But there is no Google Reader any more, and places where people share things differ a lot, twitter, weibo, facebook, etc, and someone insist on no social network.

So I begin wondering: why don’t we just share something usually on our blogs, and titled with “Share My Picks”?

Here are my picks recently, and hopefully I will see yours later.

  • How to Build a High Velocity Development Team: An article described how to build a tech team, or in author’s own words, a high velocity development team.
  • There’s More to Ruby Debugging Than puts(): Introduction of Ruby debugging, from Shopify engineering team.
  • Explain Shell: “explainshell is a tool (with a web interface) capable of parsing man pages, extracting options and explain a given command-line by matching each argument to the relevant help text in the man page.”
  • Beyond Ludicrous Speed: This is a Rails pull request for improving performance. I once gave a talk about Writing Fast Ruby at my previous company FreeWheel, and also did a same talk for current team. This is basically the real world practice for improving Ruby perfornce, expecially a library(or framework) used by so many others like Rails.
  • mycli: “Mycli is a command line interface for MySQL, MariaDB, and Percona with auto-completion and syntax highlighting.”
  • pgcli: “Pgcli is a command line interface for Postgres with auto-completion and syntax highlighting.”
  • ccat: “ccat is the colorizing cat. It works similar to cat but displays content with syntax highlighting.” It’s written in Go.
  • Is it vulnerable?: Upload your Gemfile.lock, and it will tell you what’s vulnerable and how to update it.
  • How DNS works?
  • Relative Memory Access Speeds

See you next time.

Convert Hash to XML in Ruby

| Comments

How to convert a Hash instance to XML? This is a pretty simple question, actually. Implementing it by yourself won’t take you too many lines of code actually.

Let’s say we would like to construct a XML with no nodes having attributes on them. Here are some simple codes.

(converter_simple.rb) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
require "minitest/autorun"
require "builder"

class Converter
  XML_DECLARATION = %q(<?xml version="1.0" encoding="UTF-8"?>)

  def self.to_xml(h)
    raise unless h.kind_of? Hash

    h.inject("") { |s, (k, v)| s += node_with(k, v) }
  end

  private

  def self.node_with(k, v)
    case v
    when Hash then node_with_hash(k, v)
    when Array then node_with_array(k, v)
    else node_with_string(k, v)
    end
  end

  def self.node_with_hash(k, v)
    node_with_string(k, to_xml(v))
  end

  def self.node_with_array(k, v)
    v.map { |elt| node_with(k, elt) }.join("")
  end

  def self.node_with_string(k, v)
    "<#{k}>#{v}</#{k}>"
  end
end

class TestConverter < Minitest::Test
  def setup
    @profile = {:name => "Larry", :twitter => "@larrylv", :github => "@larrylv"}
  end

  def test_string_values
    expected_value = <<-XML
      <name>Larry</name>
      <twitter>@larrylv</twitter>
      <github>@larrylv</github>
    XML
    assert_equal format_xml_string(expected_value), Converter.to_xml(@profile)
  end

  def test_hash_values
    @profile[:blog] = {
      :tech     => "blog.larrylv.com",
      :journey  => "journey.larrylv.com",
      :homepage => "larrylv.com"
    }
    expected_value = <<-XML
      <name>Larry</name>
      <twitter>@larrylv</twitter>
      <github>@larrylv</github>
      <blog>
        <tech>blog.larrylv.com</tech>
        <journey>journey.larrylv.com</journey>
        <homepage>larrylv.com</homepage>
      </blog>
    XML
    assert_equal format_xml_string(expected_value), Converter.to_xml(@profile)
  end

  def test_array_values
    @profile[:blogs] = {:blog => %w(blog.larrylv.com journey.larrylv.com larrylv.com)}

    expected_value = <<-XML
      <name>Larry</name>
      <twitter>@larrylv</twitter>
      <github>@larrylv</github>
      <blogs>
        <blog>blog.larrylv.com</blog>
        <blog>journey.larrylv.com</blog>
        <blog>larrylv.com</blog>
      </blogs>
    XML
    assert_equal format_xml_string(expected_value), Converter.to_xml(@profile)
  end

  private

  def format_xml_string(s)
    s.split("\n").map(&:strip).join("")
  end
end

The implementation is enough in many cases, but there are some obviously not good aspects.

  • It doesn’t support indentation, so the generated string is basically unreadable.
  • It doesn’t support adding attributes for nodes.

There is a gem called builder by Jim Weirich (RIP) which provides a simple way to create XML markup and data structures. With which the previous two problems would all be solved. But still, our goal is to convert a Hash instance to XML, so we should do some efforts to convert Hash’s key and value to Builder’s DSL.

First, let’s adapt Builder’s DSL to fix indentation problem.

Test Runner for Rails 5

| Comments

With Rails PR #19216 by @senny and @arthurnn merged into Rails master branch, Rails will have a test runner for Rails 5.0. And it’s awesome.

Remember @tenderlove wrote a blog to describe his experience with Minitest and RSpec, and he listed things he disliked about Minitest?

Well, it will work now if you are using rails test runner on the command line. But of course it has to be a Rails app, and specifies Rails version to github master branch.

Global Variables in Rails

| Comments

In a Rails application, sometimes you may wanna use global variables for every request, with which you don’t have to send the object as a parameter everywhere, like current_user for model layers (which may not be a good idea).

So how could we do that?

Ruby Global Variables

If you know Ruby well, you may know variable with a beginning with $ is global.

But global variables with $ prefix are supposed to be accessible from every single palce of your code, so they are shared among all threads, and that’s definitely not what we want, right?

So basically, don’t use Ruby native global variables, ever.

Objects Serialization in Rails

| Comments

Let’s talk about serializing objects in Rails today.

So, what’s the difference of serialization between in Ruby and in Rails?

In short, Rails uses ActiveSupport::JSON module to deal with JSON instead of using gems directly.

Rails 3.2 & Rails 4.0

So why would we talk about Rails 3.2 version since Rails 4.2.0.rc1 is released and Rails 5.0 development has begun?

Because we still have applications using Rails 3.2, and it’s really such a pain to upgrade apps with so many complicated logics like us.

ActiveSupport::JSON

ActiveSupport::JSON module provides a super simple API composed by two methods:

  • ActiveSupport::JSON.encode(object)
  • ActiveSupport::JSON.decode(string)

ActiveSupport::JSON.encode(object) takes a Ruby object as value and returns a JSON-encoded string. On the opposite, ActiveSupport::JSON.decode(string) takes a JSON-encoded string and returns the corresponding Ruby object.

Objects Serialization in Ruby

| Comments

Let’s talk about serializing objects in Ruby today.

Built-In Serialization Mechanisms

Ruby has two object serialization mechanisms built into the lauguage. One is what we are very familiar of, YAML(YAML Ain’t Markup Language), which is also human readable format, and the other one is binary format.

YAML Serialization

In Ruby, any objects can be serialized into YAML format. And it’s really easy:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
require 'yaml'

class A
  def initialize(string, number)
    @string = string
    @number = number
  end
end

class B
  def initialize(number, a_object)
    @number   = number
    @a_object = a_object
  end
end

class C
  def initialize(b_object, a_object)
    @b_object = b_object
    @a_object = a_object
  end
end

a = A.new("hello world", 5)
b = B.new(7, a)
c = C.new(b, a)

serialized_object = YAML::dump(c)

puts serialized_object

d = YAML::load(serialized_object)
require 'pry'; binding.pry

And the serialized_object looks like this:

1
2
3
4
5
6
7
--- !ruby/object:C
b_object: !ruby/object:B
  number: 7
  a_object: &1 !ruby/object:A
    string: hello world
    number: 5
a_object: *1

How Did Tenderlove and Others Speed Up Rails?

| Comments

Rails 4.2.0 beta1 was released August 20, 2014. And according to dhh’s release post, and I quote,

a lot of common queries are now no less than twice as fast in Rails 4.2!

So, what did Rails team – or more specifically – tenderlove (Aaron Patterson) do to improve Rails/ActiveRecord so much? Let’s find out through some commits.

Performance Tools

Here are some tools Aaron has used for measuring performance according to his Cascadia Ruby 2014 talk:

You should definitely checkout these tools. It would be very useful in your daily Ruby/Rails development.

Do You Have a Reason to Rejoice Recently?

| Comments

我似乎没有。

这大概源于我平淡无奇没有太多挑战的工作,也可能是因为几乎两点一线的生活,又或许就是我这个人的性格原因。总之,一切都是趋于平静的。当然,这对我来说也未尝不是一件好事。

但我又是一个不安于现状的人,总是渴望着改变。在学校时渴望着去公司历练,大三刚结束的暑假就来了北京,先是腾讯又是百度实习了一整年,也就此开始了北漂;毕业后在百度呆了半年多又觉得大公司的工作太没吸引力,还是跟朋友们一起出去闯闯来得痛快,就想也没想得辞职过上了没有收入的创业生活,多少有些鲁莽,但是也确实是我能干出来的事儿;散伙后就来了现在的公司,期间也想过上真正的 freelance 生活,却还是被 leader 留下,一呆就是两年。

还是我常说的那句话,人短短一辈子,何必在一个地方停留太久?我们总是被各种各样的理由羁绊与束缚,往往被蒙住了双眼,忘记了初心。