00:00:21.000
the new features of Ruby 2.0 the next major version of
00:00:27.400
Ruby so this talk will be a pure Ruby talk I mean not a rails talk um sorry
00:00:36.040
for the bit misleading title all right um my name is Akira
00:00:44.160
matsuda a matsuda on GitHub probably probably known as a author of
00:00:50.039
kamary um and a an Mata on Twitter uh became a member of the C C team since
00:00:57.760
one year ago in New Orleans dur during the um Ruby conf last year
00:01:05.320
and I'm the founder of I believe the most active um Japanese Ruby User Group
00:01:12.080
in Japan named aaa. RB so let's
00:01:17.119
begin as I'll told you I'm going to talk about Ruby 2.0 riby 2.0 is plan to be released on
00:01:26.600
the next February well it's just three or four months since
00:01:32.040
today and Matts once stated that Ruby 2 point will be 100% compatible with um
00:01:40.840
current stable verion in Ruby wow who can believe
00:01:46.840
it at least to be honest I don't but anyway um whether or not you
00:01:54.560
believe or not the next Ruby is um said to be 100 comp compatible with the
00:02:02.240
current stable version Ruby which means any of your existing application would
00:02:08.679
should still work just work with Ruby 2.0 without making any
00:02:14.879
change and not only that Ruby 2. has
00:02:20.000
absolutely awesome new features lot of new features on which I'm going to focus on
00:02:27.480
today so that means in total it's a 200% Ruby you know
00:02:36.560
that's why we call it Ruby 2.0.0 so the new
00:02:44.239
features we just had the feature freeze event last week so what I'm talking today will
00:02:51.000
basically be included in the 2.0 table today I'm going to talk about
00:02:58.680
these four big features particularly refinements module prepend innumerable
00:03:05.440
lazy and keyword arguments I'm going to describe their
00:03:11.040
I'm not going to describe their internals deeply but instead I'm going
00:03:16.159
to tell you about some stories behind the each of these
00:03:21.519
features and I'm going to show you some useful real word sample
00:03:27.799
codes sorry as for the internals don't ask me ask
00:03:33.720
kichi where is kichi
00:03:42.200
yes so the first one refinements the feature called
00:03:47.439
refinements was created by Sho MAA I guess many of you still
00:03:53.879
remember that he talked about his plan of this feature at ruon um
00:04:00.519
I think it was 2010 I think the author Shugo
00:04:07.519
is he's Matt's boss at the company named naacl that
00:04:14.760
means he's one of the highest Majesty in the Ruby
00:04:20.639
community and he's also a chairman chairman of Ruby Association Ruby o. JP
00:04:31.479
which runs Ruby Grand program and host a conference named um the Ruby World
00:04:40.120
Conference which will be held in the Ruby City Matsu in next week in
00:04:47.039
Japan oh and he recently joined um User Group aa.
00:04:53.320
RB so refinements refinements are in short um equ favor way of monkey
00:05:04.080
patching um as a background there is a problem that monkey
00:05:10.039
patching by traditional class opening is considered to be sometimes dangerous or
00:05:17.039
even harmful because it affects all instances in the um Ruby process for
00:05:26.840
example while there's an inst of personis named mats and then adding a
00:05:35.360
new um new attribute onto the person class
00:05:42.080
for another person named Shugo um the new attribute will also be
00:05:49.440
added to the former mat object do you know what I mean yes so refinements are
00:05:58.520
invented to improve this situation and provide a better way of monkey
00:06:06.319
patching the feature called refinements are consist of these two two new methods
00:06:13.479
module refine and criminal using so let's see how to use them
00:06:20.199
module refin this is probably the simplest way
00:06:26.199
of using module refine you see um here in a module named
00:06:33.160
po I'm adding a new method to string class and calling it
00:06:39.759
underneath so what's the difference between good
00:06:45.080
old New a good old monkey patching and this technology um the difference is that you
00:06:54.319
cannot call the method outside of this food module right
00:07:01.280
in other words the effect of this um def say thing here um is confined to um only
00:07:10.919
the scope of this Fu module then the next method
00:07:18.440
using having the same refinement module Pro
00:07:25.840
um now you can set the ability of the module onto any other module or
00:07:33.479
class using using key in this
00:07:39.000
example um here's another class named bar um that declares using
00:07:48.039
food like that and then the same method um added by Fu can be called
00:07:57.120
inside the bar class as well
00:08:05.400
the using method can also be used in a module like this this time the bar bar
00:08:13.599
is not a class but a module and you can invoke the method say um via module
00:08:26.280
Evo now the module can of course be an anonymous module so this way you
00:08:33.919
can locally use the method um the same method without
00:08:39.279
affecting the outside outside outside world class sorry World
00:08:47.200
string so if you're familiar with JavaScript or
00:08:53.640
other kind of like f functional programming language you might think
00:08:59.120
that you can use the Lambda or PL proc for limiting the scope um the
00:09:06.279
lexical scope but unfortunately that's not true in this
00:09:12.640
example um once you use the food module in the the
00:09:20.600
Lambda um it affects all the string outside of the lambas scope actually the world string now has
00:09:29.160
this same method because um you always need need a block in order to limit the
00:09:36.079
scope of using if you need if you really need a
00:09:41.640
proc that um using refinements you can do it this way
00:09:52.399
so so that's the brief brief description of the refinements Fe feature can go
00:10:07.160
through did you get it so I guess you're
00:10:12.440
now probably wondering how it can be used in real world applications so I
00:10:18.399
just prepared several sample applications sample libraries in my GitHub
00:10:24.760
repository the first one is called rspc refinements
00:10:30.360
the story behind this library is this syntax I do prefer this Sy this
00:10:37.639
should syntax over the uh expect and block syntax in my opinion
00:10:44.880
this should think syntax is the taste of marsp I
00:10:50.440
believe but the reason the should syntax is going to be replicated nowadays is
00:10:55.600
that the should method is directly defined on the the core basic class basic basic
00:11:04.079
object class I'm sorry and just requiring
00:11:09.120
arpc um actually changes the uh test Target application which basically never
00:11:15.720
happened right so um but I just want to use this should
00:11:23.160
method only in the specs but I don't want to change the whole application
00:11:30.040
Behavior so let's see if I can solve this problem using
00:11:36.680
refinements the arsp refinements is the name of the project the code is
00:11:42.160
available in GitHub and we can also Jem install the code is like this um can you
00:11:51.600
see it what the jam does is simply undef the should and should not
00:11:59.240
at first and redefines these methods using
00:12:05.800
refinements then inject the um refinement module into arsp example
00:12:14.880
group and the before hook that's all it's super
00:12:21.720
simple and this is how it works you see the normal should method just works as
00:12:29.199
as usual but the should method is not available outside of the example
00:12:34.839
group so just bundling this
00:12:40.800
Jam onto your project will make your Alp a bit cleaner I
00:12:46.399
think next okay the next product is the one called active record
00:12:53.720
requirements the active record requirement is doing something similar to um your name is squeal
00:13:04.600
Library it lets you rewrite this active this ugly active record query as
00:13:14.040
this what it does is um it extends active record we method to take a block
00:13:21.040
and inside the block you can use some um operates
00:13:27.279
on on symbol symol instance um such as not or greater than
00:13:36.760
or like etc etc and in in this gem the column names
00:13:42.560
are symbol un like squeal and the code is unbelievably
00:13:49.279
simple thanks to refinements so here's the code again you
00:13:54.440
can also Jem install um so let's take a look the code
00:14:00.920
firstly this module that refines the symbol class
00:14:06.959
um to be able to respond to these operator
00:14:13.079
methods um oh by the way that percent I literal here is also a ruby 2.0 new
00:14:21.160
feature that composes array of
00:14:27.759
symbols what un end uh that's not an official
00:14:34.399
feature but you see what it means and then
00:14:42.000
creating another module defining the we method to which if not block
00:14:50.720
given um then just call Super at the very bottom else evils the given block
00:14:59.519
and using refinement module in the previous slide using
00:15:04.920
this then convert the result of this uh this array object into the Aral
00:15:16.800
node that's it so then finally prepending this this module
00:15:25.360
into um active Rec query me query
00:15:31.519
methods um module in active record query as for the prepend method here um
00:15:39.560
I'm going to describe about it later so this prean exact is actually some
00:15:48.279
somewhat related to the super call at the bottom
00:15:54.160
here um yes and that's it that's all as a
00:16:00.319
result the small libraries enables this you see that these symbols respond to
00:16:09.199
the not equal or greater than equal operators and then generate the SQL SQL
00:16:20.240
strings that happens only in the wear block as you see that um the last
00:16:28.560
example you see it's a really elegant way of
00:16:33.839
implementing your own internal DSL isn't it all right the next refinement
00:16:44.319
example um active support refinements as a background story I
00:16:51.399
think there are two types of two different types of libraries in active
00:16:57.720
support one is for x and rest what I mean by the rest are for
00:17:04.880
example actess support W hash active support Jon active support time with
00:17:10.760
Zone things like that these are basically um just providing their own
00:17:16.640
class libraries so um if you don't like them you don't
00:17:22.559
you you just can't ignore them but on the other hand the Core X libraries are
00:17:28.520
actually harmful or evil just by being there because
00:17:35.720
um when they're doing what they're doing is shamelessly
00:17:41.880
overwriting um the Ruby core Library so it's t libraries like
00:17:47.840
this so only just by requiring the correx this happens and when Once loads
00:17:56.320
it you know you cannot go back to the pure core Library
00:18:03.679
so ah by the way along with Ruby oh sorry rail's three-pointer release
00:18:09.640
Jeremy Camp did a fantastic refactoring on X support so that we can selectively
00:18:16.760
selectively require um the individual corex modules
00:18:22.799
for example um in this code the first one two 3 4
00:18:29.320
has access for try method but on the other hand it doesn't
00:18:35.440
have it doesn't respond today's method because um requiring the actess
00:18:42.799
support corx object try method by name um this way you can choose only
00:18:50.559
what you really need and reduce um the global potion by access
00:18:57.400
ports however this approach is still not not perfect because as I said once you
00:19:05.120
loaded the monkey patch there's no way unloading it so in this case you cannot unload the
00:19:12.440
try method um particularly if you're writing
00:19:18.240
server side Ruby applications this would be a very
00:19:25.240
dangerous um for example imagine you have a rails controller like
00:19:31.760
this it looks like um the required numeric time at the
00:19:38.720
first line enables the three days ago in the
00:19:44.679
middle so before and after someone touches this
00:19:50.400
controller the behavior of numeric class changes imagine how it's dangerous
00:20:01.919
actually um um rails framework requires access
00:20:09.039
support all by default so this code wouldn't be so dangerous indeed in your
00:20:15.720
normal ra application but you know this this case is just an example
00:20:24.440
so so how can we more effect ly and safely
00:20:30.360
load access support monkey patches um it should be wonderful if you
00:20:36.760
can arbitrarily load particular F support modules only inside certain limited
00:20:43.799
lexical scope um yes time for
00:20:49.760
appointments so this gem does the magic actess support requirements see GitHub
00:20:57.760
for the code and J is also
00:21:09.559
available this example callede by using actess support refinements at the first
00:21:16.559
line um object try is enabled only um the first
00:21:27.080
module and you see this way you can avoid disastrous Global
00:21:33.679
pollution you know what I mean I said you can stop the global
00:21:39.440
pollution that means you can save the environment you can save this planet
00:21:45.600
awesome so by using this feature the format dangerous controller can be written like
00:21:53.640
this now three days ago here just works but but it does not work outside of this
00:22:01.440
controller so that's it um please know that this active
00:22:08.240
support requirement library is um really experimental and not at all not all the
00:22:15.200
module there are working at this
00:22:22.120
moment sorry why why is dangerous
00:22:30.240
um okay because by default the your numeric
00:22:37.559
objects does not respond to uh in this case days
00:22:43.520
but once someone um sends a request to this controller and the real reals process
00:22:51.200
loads this controller then the the behavior of the numeric class changes
00:22:57.200
before and after the the request that will cause some unexpected
00:23:05.840
error so ah so oh by the way I forgot to tell you
00:23:12.600
one more important thing um refinements are experimental
00:23:19.000
and it could be reverted suddenly I'm serious
00:23:28.960
hope not all right so now it's
00:23:34.240
finished um then let's move on to the next feature and I think I spent half of my
00:23:40.440
presentation some time module prepend the story behind this is in
00:23:47.960
early Davis rails we had Alias method chain
00:23:55.559
um which was initially created by James book consider there's a class named
00:24:02.880
a having a method Fu and you want to add some new features
00:24:10.039
onto the existing Fu method um for this purpose you can use
00:24:16.679
Al method chains like this um the difference between the Alias
00:24:24.120
method chain and normal method overwriting is that
00:24:30.880
um is that you don't need to copy and paste the whole method of the original
00:24:36.159
food method make sense so as a real world real real world
00:24:43.880
example here's a very simple um code that extends active
00:24:49.679
active record fre page method I'm sorry relation class um relation all method to take a
00:24:57.799
page option to return the pated pated fa
00:25:04.000
results you see the logic here is very simple piece of
00:25:09.200
code but to tell you the truth this is actually like 90% of comary gem
00:25:18.880
do so um El method chain is certainly useful but on the other hand it's it has
00:25:27.200
some um kind of known problems now consider there's a class having two
00:25:35.279
Alis method chains in one class I'm sorry in one single food
00:25:41.000
method then um let's call one one of the
00:25:46.399
without methods named food without
00:25:51.720
B the F fu is extended by bar and bz and
00:25:56.840
I'm calling a Fu without bar method can anyone tell what's going to
00:26:03.640
happen the answer is that F without Bar Method skapes not only bar but also it
00:26:11.200
skapes bar and bz who can expect this the method named
00:26:17.399
F without bar lies it should be like f without bar and
00:26:22.520
be right so the actual examp example of
00:26:28.760
this problem is like this on a bit a little bit all active record
00:26:35.799
um actually um it uses the Al Al the
00:26:41.720
version of acod heavily used um Elis method chain so calling a method named
00:26:49.840
save without diry actually skapes not only zery but
00:26:55.520
zery and validation so um calling save without
00:27:01.559
diry immediately saves a un invalid
00:27:07.360
record into the DB this is so dangerous
00:27:13.039
and consider if you installed more than two monkey patching
00:27:19.399
plugins that El missed chaining um onto one same method the same thing should
00:27:25.399
happen it's you know it's really it's possible
00:27:35.399
and you know these um these two uh two monkey
00:27:42.159
patching plugins should invoking the without methods internally so nobody can
00:27:48.760
tell what's going to happen so this is the
00:27:56.039
problem so since three the ra score code
00:28:01.200
gradually abandoning El meth the chain and switching to more like modular
00:28:08.519
architecture for example this commit by Yehuda um totally eliminated the use of
00:28:14.760
LMS to change forac record was done in two years
00:28:20.760
ago for example like this um is just replacing the use of Lis
00:28:28.600
method chain into um method override and
00:28:33.760
super call so the format of F Bar as example
00:28:43.200
can be written like this in this way using separate modules
00:28:51.039
for uh bar and B and call Super in each
00:28:56.799
modules then include each of these modules into the um Target a
00:29:06.399
class there's a rule though or a restriction the original F method has to
00:29:13.159
call Super in other words you cannot you you can only extend methods that originally
00:29:19.559
called super but in many cases what we'd like
00:29:26.960
to do is to extend a method that does not called super
00:29:32.519
inside right so how can we monkey patch
00:29:37.720
this method without using elas method change
00:29:43.679
I said as I explained it has some risks so for this purpose now we have module
00:29:50.159
prepend in R 2.0 this feature was proposed by a
00:29:55.399
hudats and implemented by no n
00:30:04.480
no this is noo he's there and we call him the patch
00:30:12.159
monster because because he's the monster this is
00:30:17.279
the whole git log on Ruby Ruby repository that
00:30:24.679
shows um Noble is the definite number one Ruby
00:30:30.399
committer far above Mets right this is why we call him the patch
00:30:36.480
monster in short patmon
00:30:42.480
so in my opinion in my opinion Ruby is a language
00:30:49.760
that mats designs and coichi implements and noou
00:30:56.559
fixes and and note
00:31:02.600
that all of these three guys are now hired by Heroku as full-time Ruby core
00:31:10.200
committer so I really really would like to say thank you to Heroku for
00:31:16.480
sponsoring Ruby development in such efficient way Heroku
00:31:28.799
Heroku definitely pushes the uh Ruby development
00:31:36.639
board and it's so in impressive that he's always drinking something
00:31:43.240
like beard in this picture or sake or Shu or something even he's writing his
00:31:50.399
Ruby patch that is noou oh and of course noou is also a
00:31:55.880
member of Oak ARB so going back to the code the Alias
00:32:02.639
method chain in this code can be Rewritten
00:32:08.639
like this using moand um you needed another module
00:32:18.480
defining the same F method
00:32:23.720
and um just prepend the method onto the class a
00:32:34.880
right of course you can prepend multiple modules in this case bar and B into the
00:32:42.880
target class um as Matt explained this keynote
00:32:51.799
what prepend US is like it's something similar to include but the
00:32:59.200
uh water of method invoking is just reversed
00:33:05.440
so this is um modu prepend version of the former simple
00:33:12.639
pation um I think the logic is not totally the
00:33:17.799
same for some reason but anyway it's kind of working the
00:33:24.320
same just Define just create a module defining all method then prepend it onto
00:33:31.399
the active record defining methods module it's so
00:33:36.639
simple and as I already showed you there's
00:33:41.799
another example of using method module prepend in the form of active record
00:33:47.639
refinements gem so remember um this a module
00:33:53.480
defining where method and calling super inside and pre prepending that module
00:34:01.399
onto um active record query methods module you see it's far more reable
00:34:09.560
readable easier to read than elas method chain because there's no something with
00:34:15.079
something method or um things like
00:34:21.639
that okay it's done so the next feature inur
00:34:27.839
able innumerable lazy this feature was proposed by um
00:34:33.720
utak Hara he lives in a ruby City M where where Matt lives
00:34:41.079
and he's also working for na um the company where Matts and Shugo
00:34:47.560
is working as for the background of this
00:34:53.399
feature and details of this feature um just
00:34:58.520
please just see the ticket here on red mine
00:35:04.800
um basically what it does is um well in each
00:35:13.880
call on an inumerable object returns an enumerator
00:35:20.359
instance a new method on inumerable inumerable named lazy returns an object
00:35:27.440
of with um in numerator lazy
00:35:32.800
class and what you can do with this enumer enumerated lazy object is for
00:35:39.280
example like this um you
00:35:45.000
know um every enumeration method such as map or select zip grip flat map uh Etc
00:35:54.400
on this object will will be deferred until you call 2 a or for
00:36:01.520
force force is just an alas of 2 a I think so this way you can treat a
00:36:11.720
infinite collection like this if you're not using lazy um this
00:36:17.400
will this will never finish but using lazy you
00:36:23.000
can um defer the invocation of
00:36:28.319
uh select even and take only 20 um 20
00:36:34.560
numbers word like this you see it it's basically the
00:36:40.440
same it's a script that um picks up um Friday the
00:36:50.839
15 so here's another really elegant example
00:36:55.880
of using numerable lazy by uh why harz boss
00:37:02.160
sha and actually besides of this sort of
00:37:09.400
scrip um I'd be thinking of a usage of inumerable lazy in rails application
00:37:15.280
because I titled like um r2.0 in rails
00:37:20.680
but a fortunately I couldn't find any good example on Rails so far so I'm
00:37:26.920
sorry this no gem for this feature and maybe I can use it as a back end of active record qu
00:37:35.920
interface I think or maybe for example I can use on um action live controller I'm
00:37:45.480
not sure or sprockets related something I'm not sure so anyway
00:37:54.560
um I could not come up with any good example I'm sorry so please tell me if
00:38:00.560
you find any all right in this example um suo is
00:38:05.800
implementing a Ruby version of Unix R count
00:38:11.240
command um using two inumerable
00:38:20.280
lazies one is that it R over um file
00:38:25.640
names and one that um lazily differs the uh files each
00:38:33.720
lines then then counts the
00:38:40.040
worries um he says it's like um faster
00:38:45.680
and um consumes less memory
00:38:51.640
so so the next feature is keyword arguments
00:38:59.160
the keyword argument implementation was done by Yus
00:39:05.200
Enzo he is really an amazing super geeky hacker uh he's a Quin
00:39:13.079
artist um and he's the release manager of the next release Ruby
00:39:20.960
2.0 so actually he controls the management um the release of Ruby 2.0
00:39:30.839
um so the background this is a well-known and widely used
00:39:38.440
idom for imitating um keyword argumentation
00:39:44.079
thing um just use hash object like this in the method
00:39:50.440
argument and in combination with um repat 1.9 style has
00:39:59.200
syntax it perfectly looks as if there's a language native language
00:40:06.079
support on um keyword arguments it really looks
00:40:12.280
like like Objective C or C or something you know
00:40:19.119
or but we need some tweak in order to use this method in know real real world
00:40:25.319
applications um like this one is the tweaking the default
00:40:35.240
values um for example if the F method takes the
00:40:42.240
default values a defaults to one B defaults to two we do something like
00:40:48.119
this right we need to merge a default values
00:40:53.280
hash into the argument or acess support has a special command I
00:41:01.280
mean special method named reverse marriage
00:41:06.920
which some kind of um kind
00:41:12.599
of not all looks better I mean kind of
00:41:19.720
readable but it's doing um basically the same thing
00:41:25.960
so um the next problem is multiple hashes there
00:41:31.920
there there's tons of um methods inside rails that takes two
00:41:40.760
or more hash um hash arguments
00:41:49.359
so this is actually a method named Boton two takes two hash arguments one is
00:41:56.680
option and the other is had HTML options and if you would like to if you want to
00:42:02.560
pass in a any option into the last L HTML options
00:42:11.160
you need to you need to um write the uh
00:42:17.319
you know hash Le R onto the first action new parameter otherwise the
00:42:24.960
whole arguments will put into the first hash so this kind of
00:42:31.839
looks ugly
00:42:38.000
and handling slat operative um when
00:42:44.000
the uh method having Splat operator that
00:42:49.720
takes um VAR variable arguments um and
00:42:58.119
yes this is this um often happens like um and takes the very
00:43:06.040
last um arguments of the Splat op option as the hash so this does this ex
00:43:16.240
extract option also um defines an ex support like this it just if the are
00:43:25.240
given arguments less is a hash then treat it as a
00:43:31.760
hash and last week um assert valid Keys
00:43:37.680
um in order not to accept a
00:43:44.599
valid argument Keys there's a method called assert valid keys in active
00:43:52.319
supports this is the implementation of assert valid keys
00:43:58.640
so simple and this is how you how it works
00:44:04.839
right so we need um this kind of lot of
00:44:11.119
um tweaks or work around in order to treat the has hash
00:44:18.960
argument so the Ruby 2 point keyword arguments are H I only have one more
00:44:26.240
minute sorry so um it reads default values it
00:44:34.800
does and it can also treat multiple hashes
00:44:39.920
and SPL operator like this
00:44:46.280
and it includes assert valid Keys
00:44:52.079
um by default right so
00:44:57.960
we need none of them we need none of these work arounds
00:45:03.640
yeah the Ruby 2.0 keyword argument just does it so the
00:45:11.559
conclusion um everyone can upgrade Ruby version immediately
00:45:18.240
or I mean if you're brave enough you could do so
00:45:23.599
immediately I mean the SVN trunk version is also aimed to be totally compatible
00:45:29.119
with Ru one or3 so if any of your existing
00:45:34.559
application um except for C extensions does not work on Ruby trunk that is a
00:45:40.760
ruby bug so if that happens to you you're so lucky that you h a chance to contribute
00:45:48.119
to Ruby 2.0 then please file a
00:45:53.240
ticket onto red mine if you concert any such a
00:45:58.920
problem or else if you're not if you're still not using 1
00:46:04.800
193 um Now's the Time to move because ruby8 support will be dead
00:46:12.440
very soon you should abandon your your B8 application as soon as
00:46:20.280
possible and standby for the common riew
00:46:25.559
2.0 so we encourage you to use R 2.0 and use these new features because it's
00:46:32.200
useful and fun um you know if everyone uses these new
00:46:40.240
features then we'll probably um speed up the transition to Rie
00:46:46.280
2.0 so let's use 2.0 and let's deprecate one3 as soon as
00:46:53.760
possible that's all thank you