5 random ruby tips

8
5 Random Ruby Tips

Transcript of 5 random ruby tips

Page 1: 5 random ruby tips

5 Random Ruby Tips

Page 2: 5 random ruby tips

about me● Akira Hirakawa

● VP Engineering at Burpple

● Ruby, Go, iOS Engineer

● https://github.com/akirahrkw

● http://www.akirahrkw.com

Page 3: 5 random ruby tips

1: levenshtein distance● measure the difference between 2 sequences

● “Ruby” and “Rugby”

● can be used to implement suggest feature

● http://en.wikipedia.org/wiki/Levenshtein_distance

● https://github.com/akirahrkw/levenshtein-distance

distance = "Ruby".levenshtein_distance("Rugby") # distance is 1

distance = "saturday".levenshtein_distance("sunday") # distance is 3

distance = "akira".levenshtein_distance("hirakawa") # distance is 5

Page 4: 5 random ruby tips

2: Rails4.2 + jsonb● Rails4.2 supports jsonb type in Postgres 9.4

● jsonb vs json

○ jsonb is binary, json is string

○ search is faster than json, storage size is bigger than json

○ if you prioritise storage efficiency, use json

○ if search efficiency, use jsonb

● we haven’t used jsonb in practice yet :(

● http://www.postgresql.org/docs/9.4/static/datatype-json.html

Page 5: 5 random ruby tips

3: protected method● protected method can be called with a receiver

● but the receiver must be in the inheritance hierarchy of the caller

Page 6: 5 random ruby tips

4: each● concurrently do each multi-array

keys = [1, 2, 3, 4, 5]values = [‘ruby’, ‘swift’, ‘objc’, ’java’, ’python’]keys.zip(values).each do |key, value| p key

p valueend

Page 7: 5 random ruby tips

5: frozen constant● constant is mutable

module CODE LANGUAGES = [“ruby”, “python”, “objc”, “swift”]endmodule CODE LANGUAGES = [“ruby”, “python”, “objc”, “swift”].freezeendmodule CODE LANGUAGES = [“ruby”, “python”, “objc”, “swift”].map!(&:freeze).freezeend

Page 8: 5 random ruby tips

Thank you