00:00:13.920
hey everyone um let's uh continue our
00:00:17.520
talks for today um beside me is
00:00:20.720
Christine uh she is our first lady
00:00:24.119
presenter of the
00:00:26.199
conference
00:00:28.320
yeah uh fact she is a fellow cat
00:00:33.640
lover okay um I will not dist her
00:00:36.680
anymore so please um Round of Applause
00:00:39.520
for her talk thank
00:00:44.440
you hello uh so girl
00:00:48.719
power so my talk is about Ruby Golf and
00:00:52.680
I would like to encourage everyone to
00:00:54.600
play Ruby golf as well and you can I'm
00:00:58.320
again I'm Christine Joy p you can find
00:01:00.399
me on GitHub at kjc pass um my Twitter
00:01:04.479
Handler is the same but actually I don't
00:01:07.600
open my Twitter account so it's useless
00:01:09.880
don't bother with
00:01:11.479
it so I'd like to introduce myself first
00:01:15.040
and as Natalie said I'm a cat lover and
00:01:18.759
at home it's like I'm playing Neco atum
00:01:21.079
every day so that's two of my cats and
00:01:25.000
even in my uh console I have this cat
00:01:27.920
Emoji so yeah I love
00:01:30.920
cats so I am from the Philippines anyone
00:01:34.759
else from the Philippines here raise
00:01:36.200
your hands oh white many so nice to see
00:01:40.439
you here and actually before I went to
00:01:43.520
Singapore I was in Malaysia for a couple
00:01:45.479
of days
00:01:47.360
so they made a mistake in my name in
00:01:49.880
Malaysia they thought I'm Joey so I am
00:01:52.719
not Joey I am joy j o y just three
00:01:55.320
letters okay and I work for quipper and
00:01:59.920
edtech company uh in Manila and just a
00:02:04.680
little plug we are hiring so if you are
00:02:07.079
from London Tokyo Manila Jakarta or
00:02:10.360
Mexico uh you can apply in some
00:02:13.280
developer positions you can visit our
00:02:16.000
website and we also have a tech blog I
00:02:18.920
also wrote some articles in there about
00:02:21.160
some problems we've encountered and how
00:02:23.000
we solve them and you can approach me
00:02:25.640
every time so going back let's play Ruby
00:02:30.599
Golf and though we won't be playing it
00:02:33.120
like this it's just too expensive but
00:02:36.040
it's a it's the wrong type of golf so
00:02:39.920
the Ruby golf that we'll be I'll be
00:02:41.879
talking about is it is a practice when
00:02:45.480
one tries to achieve the shortest
00:02:48.159
possible code that that solves a certain
00:02:50.920
problem and in this case it's using the
00:02:53.319
Ruby language but this practice is not
00:02:56.440
exclusive to Ruby um you can also see it
00:02:59.480
in Pearl JavaScript and many other
00:03:02.680
languages and if you're interested you
00:03:05.480
are you can visit the Anarchy golf site
00:03:08.280
in golf. shh.org
00:03:11.280
so the website looks a bit ancient but
00:03:14.920
there are more than 900 problems in
00:03:17.760
there so you can check it
00:03:20.680
out so for if for an overview of golf
00:03:26.159
let's try the fsas and if you're not
00:03:28.720
familiar with f bus so if you're given a
00:03:31.680
number n so you have to iterate from 1
00:03:33.680
to n if it's divisible by three print
00:03:36.560
fiz divisible by five buzz by both fiz
00:03:41.480
Buzz otherwise print the number so if we
00:03:44.760
try to solve it straight forward uh so
00:03:47.439
sorry we're going to run the program
00:03:49.920
using the Ruby command and take note
00:03:52.319
that we can get the parameters by using
00:03:56.200
RV so for the beginners out there
00:04:00.560
there so the output should look like
00:04:03.560
this so since one and two are not
00:04:06.560
divisible by three or five they are
00:04:09.360
printed out and on three instead of
00:04:11.920
printing three it prints Fizz and so
00:04:15.160
on so let's try solving the problem and
00:04:19.320
the straightforward
00:04:20.880
solution would look like this you have a
00:04:23.400
loop and a series of conditions so it's
00:04:27.880
very clear right
00:04:30.560
so very easy and it's like you just
00:04:33.840
rewrote the problem
00:04:36.160
statement but now let's look at the
00:04:39.639
shortest possible code this is not my
00:04:42.360
code by the way I just search shortest
00:04:45.000
fbus and my reaction was this at first I
00:04:49.479
didn't understand anything let's look at
00:04:53.080
take a closer look inside the
00:04:55.880
loop what's the hell is that so there's
00:04:59.880
some string interpolation and some AR
00:05:02.880
processing and lots of ores but if you
00:05:05.919
take a look at it for a very long time
00:05:08.800
you might be able to understand
00:05:11.479
it
00:05:13.280
well the first kind of code is what a
00:05:16.479
professional code might look like it's
00:05:18.680
very clear and if you look at the
00:05:20.440
variables you understand what it's
00:05:22.639
for so like that descriptive naming
00:05:26.080
clear and understandable it's
00:05:27.840
maintainable and it's also so easy to
00:05:30.080
debug because if there's an error in
00:05:32.400
your code you will just see oh there's
00:05:34.720
an error in line seven error in line
00:05:37.080
nine so you can just check those lines
00:05:40.319
now for the golf code it's single letter
00:05:43.639
naming so what is X what is n it's I
00:05:46.639
don't know so because of that it's very
00:05:50.520
hard to understand and it's not
00:05:52.880
maintainable and it follows that it's
00:05:55.120
also hard to debug because if you have
00:05:58.000
an error it's says you have an error in
00:06:00.720
line one but everything is in line one
00:06:03.680
so where you have to check
00:06:07.639
everything
00:06:09.599
so one one more thing if your teammates
00:06:13.800
or your boss see you doing golf code you
00:06:15.880
might get
00:06:17.120
fired so why do we need to do Ruby golf
00:06:22.880
well for it is to know some Ruby Secrets
00:06:26.080
or the Lesser known uh features of Ruby
00:06:31.120
so the first secret I discovered were
00:06:35.039
were predefined variables you can go to
00:06:38.280
this site or you can just search so like
00:06:41.680
in the golf code uh it's using dollar
00:06:45.880
asterisk so it's actually a short time
00:06:48.080
for argv and these are some of the other
00:06:51.039
variables that uh we have there are a
00:06:55.199
lot more so you can just look at the
00:06:58.479
documentation
00:07:00.240
and another example uh this dollar
00:07:02.879
greater than is an alternative for the
00:07:06.000
default output which is STD out so if
00:07:09.280
you push some string into it it would
00:07:11.960
print the hello world for
00:07:15.599
example now the second secret is the
00:07:18.960
question mark we usually see this symbol
00:07:23.879
when uh we use methods that return
00:07:27.840
Boolean values like to that even
00:07:31.560
question mark or a intern
00:07:35.680
operator but another usage of question
00:07:38.759
mark is to Define Single Character
00:07:42.240
string literals like question mark a
00:07:45.720
instead of quotation
00:07:47.240
a and you can even use it in symbols
00:07:50.280
like in the third one question mark dot
00:07:54.199
is like quotation mark dot so it saves
00:07:59.000
you to one character so three two
00:08:01.720
characters instead of two so in golf
00:08:03.960
code shorter is
00:08:06.280
better next is the
00:08:09.039
asterisk you we usual usage is for
00:08:13.440
multiplication and splat operator and
00:08:17.400
the Splat operator usually see in method
00:08:21.840
arguments so it just transforms the
00:08:24.840
arguments into an
00:08:27.199
aray another secret use AG of asterisk
00:08:30.680
is it's an alternative to join at first
00:08:34.320
when I know this it's like why are you
00:08:37.440
multiplying AR by a string it doesn't
00:08:40.839
make sense
00:08:43.320
but well as maybe as I did golf it
00:08:49.519
started being natural for me so one
00:08:53.240
character versus four characters that's
00:08:56.600
a
00:08:57.760
lot next
00:08:59.720
and actually this is my favorite is the
00:09:03.720
square brackets usually it's for getting
00:09:06.920
elements of
00:09:08.720
array hashes strings and other
00:09:11.959
innumerables
00:09:14.279
another thing it's secret usage is it's
00:09:19.120
an alternative to Strings include method
00:09:22.519
so if you apply a substring uh apply
00:09:28.560
square bracket get to a substring it
00:09:30.680
would check if that substring exists in
00:09:32.800
the string so like in the first example
00:09:35.440
e exess exists in hello that's why it
00:09:39.160
returned
00:09:39.920
eel on the second one hello doesn't have
00:09:43.880
M so it returned nil and as a bonus it
00:09:48.079
can even check regular expressions like
00:09:51.480
on the third one it's checking for
00:09:53.480
capital letters uppercase letters and it
00:09:56.200
return the capital H on the last one is
00:09:59.480
checking for Whit space characters but
00:10:02.279
there's no Whit space characters so it
00:10:05.279
returned
00:10:06.839
nil does anyone did anyone know
00:10:11.399
this seems like it's the first time for
00:10:15.360
most people to know this
00:10:18.000
so it's it can be useful even in some
00:10:22.640
other
00:10:24.279
functions next are unary operators I'd
00:10:27.959
like to focus on on two SIM unary
00:10:31.320
operators specifically the negative and
00:10:33.959
the complement so negative compl of a
00:10:38.279
it's equivalent to a minus one and if
00:10:41.079
you just reverse it it's equivalent to a
00:10:43.440
+ 1 so maybe you might ask what's the
00:10:47.920
benefit of one over the other if they
00:10:50.000
have the same
00:10:51.560
length well that's because unary
00:10:54.240
operators have higher precedence over
00:10:56.320
other operators so you we have
00:11:00.519
pmas but it's like you just added u in
00:11:03.959
front so it it uh gets done before the
00:11:08.519
other operators like in the first one
00:11:11.760
it's like you're multiplying 3 + 5 which
00:11:14.440
is 15 but if you you use 4 + 1 3 * 4 uh
00:11:21.079
gets multiplied first before adding one
00:11:24.240
so it would result to result to 13 which
00:11:27.440
is wrong and if you try to replicate the
00:11:31.120
same behavior using 4 + 1 you'll have to
00:11:33.399
add
00:11:34.880
parenthesis and so we can reduce the
00:11:38.920
number of characters in that
00:11:42.360
case and there are some other techniques
00:11:47.399
maybe this is quite common sense you can
00:11:51.040
use stary operators instead of the usual
00:11:53.880
if
00:11:55.160
else use single line block syntax in
00:11:58.959
instead of do end and in checking the
00:12:02.279
visibility you can check if it's less
00:12:04.680
than one instead of checking if it's
00:12:06.240
equal to zero it saves one
00:12:10.440
character and similarly using map
00:12:13.480
instead of each uses less one less
00:12:17.600
character and uh map uh does whatever
00:12:22.120
each does and if it's possible you can
00:12:26.440
use the exact value instead of computed
00:12:29.519
value like
00:12:32.399
3.1416 instead of math Pi other
00:12:35.839
approximations can also be used but it
00:12:38.079
requires some research so I had to look
00:12:43.160
to to search in
00:12:46.360
Google about these
00:12:49.320
approximations now let's try applying
00:12:52.040
what we learned in Caesar Cipher anyone
00:12:56.079
familiar with Caesar ciphers
00:13:01.160
so it's one it's I think it's the
00:13:03.639
earliest known substitution Cipher in
00:13:06.279
existence so it's like you're Shifting
00:13:09.320
The Alphabet by a certain amount and
00:13:11.800
then you just map the letters like here
00:13:15.199
if it's it's shifted by three and hello
00:13:18.040
transforms into core k h o o r and to
00:13:22.399
make it simple we'll just ass let's just
00:13:25.240
assume that the text is only composed of
00:13:27.320
letters uppercase or lower
00:13:30.519
case and this is what I this is the
00:13:35.519
solution I came uh
00:13:38.519
I I did for the CES Caesar Cipher and I
00:13:43.600
did a character count and it has 248
00:13:48.240
characters so my goal is to reduce it by
00:13:53.079
75% to around 60 let's see if we can do
00:13:57.440
it so first thing that we can do is to
00:14:01.519
change variable names into single
00:14:04.600
letters
00:14:06.360
so and it
00:14:08.959
becomes a lot less understandable so
00:14:12.560
what is a what is B what is C Etc but we
00:14:17.199
were able to uh
00:14:19.839
reduce uh to shave off 68 characters
00:14:23.600
just from that and next we can use some
00:14:26.720
Shand like dollar star instead of RV and
00:14:30.920
we can use a and b directly so if you're
00:14:34.560
familiar with algebra it's like you are
00:14:37.120
substituting the value of a and
00:14:41.160
b oh it
00:14:44.000
becomes like
00:14:47.399
this sorry if I'm going too
00:14:50.440
fast and similarly we can use D and O
00:14:54.000
directly and we can remove the comments
00:14:56.160
because you know they are not needed in
00:14:59.079
golf golf is made made to not be
00:15:04.680
understandable and now we reduced it to
00:15:07.680
four lines and 104 characters and it's
00:15:11.519
getting and getting more
00:15:14.000
cryptic and next we can use the question
00:15:17.680
mark a instead of quotation mark a but
00:15:20.959
even better we can use 65 directly
00:15:24.040
instead of a. or because it's what it
00:15:27.240
evaluates to anyway
00:15:31.600
and now we're down to 94 characters and
00:15:35.399
we can use this method called bites in
00:15:39.440
string I only know about this method
00:15:42.160
when I did golf so it's like you are
00:15:45.680
mapping each character of the string in
00:15:49.639
uh with
00:15:52.920
or uh so lot of lots of
00:15:56.959
steps but
00:15:59.319
it results into this 70 characters and
00:16:02.839
lastly we remove the spaces they're not
00:16:06.199
needed yeah and that's the final code
00:16:10.319
that we have and we are down to 64
00:16:14.120
characters and we were able to reduce
00:16:17.920
The Code by 184 characters
00:16:21.199
74.2% my goal was
00:16:23.440
75% didn't make it but it's close
00:16:27.519
enough well actually it's possible to
00:16:31.000
reduce it further but uh I decided to
00:16:34.800
stop here because
00:16:37.959
maybe it's it will not be really
00:16:42.680
understandable and in
00:16:45.920
conclusion what ruby golf made me do was
00:16:48.880
I read more Ruby documentation like uh I
00:16:52.360
wouldn't have discovered bytes if I
00:16:54.720
didn't uh read more documentation that I
00:16:59.279
did I
00:17:00.519
don't that I don't usually
00:17:03.079
visit and the main goal is to search for
00:17:06.640
methods and variables with short names
00:17:09.319
short is
00:17:11.199
better uh it made me think of ways to
00:17:14.199
solve problems
00:17:16.480
unconventionally so this fiz buus code
00:17:19.480
even though it's not mine I
00:17:21.799
just I realize
00:17:24.280
that um the
00:17:26.959
Ingenuity that people can have so this
00:17:31.000
is something that most people won't
00:17:32.520
think
00:17:33.400
about next it made me a reg
00:17:37.120
expert I I reference rub.com a lot less
00:17:41.880
when I did a golf code because if you're
00:17:45.080
doing golf you get to use regular
00:17:47.760
Expressions a
00:17:50.000
lot and next it challenge my logic and
00:17:53.760
creativity so this code was actually
00:17:57.000
from the last Rubik
00:17:59.159
from a contest that they have so it's
00:18:01.360
like there is a this is a Sudoku solver
00:18:05.159
and the puzzle is embedded inside the
00:18:07.520
code so I would like this is like the
00:18:09.440
next step so it's like you're making
00:18:13.200
code with art and I the first step is if
00:18:16.640
you know how to shorten your code then
00:18:20.320
you can manipulate your code to look
00:18:24.000
something like a painting so I would
00:18:27.080
like to do that
00:18:28.919
someday and like Matt said uh we should
00:18:33.159
always have joy in programming and uh
00:18:36.360
Ruby golf provides new challenges and
00:18:39.840
yeah it gives more joy in
00:18:42.760
programming so let's all play
00:18:45.919
golf thank
00:18:52.200
you do you have any
00:18:56.720
questions uh we have time for one
00:19:03.480
question
00:19:11.480
anyone hello which one which was your
00:19:14.880
favorite golf
00:19:16.320
trip
00:19:18.039
um yeah my favorite was the is the
00:19:22.039
square brackets especially because it
00:19:25.280
can even process regular EXP Expressions
00:19:29.480
so it's very it can kill two birds with
00:19:32.799
one stone thank
00:19:37.919
you um thank you
00:19:42.360
J