I think that Ruby Kaigi is one of the most amazing programmer conference.We will show the fun of Ruby Kaigi and the power of Ruby Kaigi.We will talk about hijacking Ruby syntax in Ruby.Ok, at first I introduce myself.ともひろはしだて日本から来てくれます私の友達はジョーカーさん私のTwitterアイディTwitterハブアイディジョーカー1007ぜひジョーカーを覚えてください私はCTOでリプロイングをしています私はデータエンジニアインフラエンジニアウェブアプリケーションエンジニアセトバロビーロビーオンライルフルエントリーフレストセトバこのルビーカンフ初ルビーカンフ エクスペリエンス初ルビーカンフウェブアプリケーションこの話は私の初ルビーカンフ初ルビーカンフ私は私の初ルビーカンフ私は私はロビーカンフの話をしています素晴らしい次はトーキングパートナーモリスタンこんにちは私はサトシタゴモリ多くのルビーカンフモリスタンモリスタン私はサトシタゴモリスタンオーサーマンテナープロジェクトフルエントリーウェブアプリケーションウェブアプリケーション私はサトシタゴモリスタンサトシタゴモリスタンサトシタゴモリスタンサトシタゴモリスタンサトシタゴモリスタンサトシタゴモリスタンサトシタゴモリスタンサトシタゴモリスタンサトシタゴモリスタンサトシタゴモリスタンサトシタゴモリスタンサトシタゴモリスタンサトシタゴモリスタンサトシタゴモリスタンサトシタゴモリスタンサトシタゴモリスタンとても面白いプログラミングのテクニックについてメタプログラミングについてとても面白いコードについてメタプログラミングについてこの話についてLuby Standard Featuresについて私たちのコードについてメタプログラミングについてメタプログラミングのテクニックについてまず、ビデオについてビデオはコンテクトのオブジェクトについてローカルバリアボールとインスタンスバリアボールと多くの人々のバリアボールを使ってビデオについてテンプレートエンジンを使ってこのサンプルコードがビデオを使ってメタプログラミングのコンテクトでビデオについてバリアボールを使ってローカルバリアボールとインスタンスバリアボールのサンプルコードについてローカルバリアボールのタイミングについてビデオのクラスを使ってローカルバリアボールと local variables, local variable get, local variable definedand local variable set.So this code shows what we can do using binding.In this code, this code gets binding object from binding proxy methodswith variable a, b, and cand also dumps the contents of binding object.And also this code is doing a local variable get methodto get the value of variables a, b, and cand dump them.And this result shows that the binding objectis recreated par binding code.So that means if we do a local variable seton our binding object acquired from different contextand we can see the newly created local variable djust after local variable setbut in original context the local variable d disappeared.So that means we cannot create a new local variableon different context.Interestingly, this code is doing local variable seton both of local variable dand awith value integer 20.So the result shows thatand we can see both of local variable dand awith integer 20and also in original contextwe can see local variable a with integer 20.This means binding local variable set methodis to add a variable only in a binding instanceor to overwrite values of existing differentexisting variables in original context.We can do that.Next is a tracepoint.The sample code shows that the tracepointis to trace events in Ruby virtual machineand call the hooks about these events.We can hook many events like a line,layers, and then cross and end for the beginningof class definition and the end of class definitionand call and return about to hook the method codea Ruby method codeand she call and she returnabout the methods implemented in C languageand the b call and b return for blocksand thread begins thread end and fiber switch.Ruby document says thatand we can usetracepoint to gather informationspecifically for exceptionsbut of courseand we can useand many various eventsthat document is completely wrongand anyway.So look at this codeand this codeis very simple and a method definitionto define a with argument aand dumpthe content of local variable aand then return a stringyayin global casesand then calla method with integer 100and then dumpsthe result of that method.So result is surprisinglythe value of local variable a is string100and the return value is upcaseto a. What happens?In fact, above of the codeI define theI enabledtracepoint to setand local variable with the stringfiedvalue and to callupcase method on return value.So this is what tracepoint can do.So the tracepoint having many methodsto control its behaviors or to knowthe details of events,what is the eventthe event occursand we have somemethods to know the details of eventslike a method ID or a curry IDor less exception or return valueand tracepoint also have a methodbinding.That means we can getbinding object at any pointwe can hook usingtracepoint.So that means we canuse tracepoint to gather informationabout ruby virtual machine or we canoverwrite every local variablesat any time in ruby code.So you should not require my codebecause I can break any kind of ruby codeyour ruby application,your ruby libraryanytime,completely.Yes, I can. Trust me.Anyway,I will talk about two moving featuresfirst,it's refinement.Refinement provides a way to extendthe class locally.It's useful tomultipatch safety.This code isthis is a sample of safetymultipatch.But I'm sadbecause most ruby programmersdon't use this feature.In fact,refinement is difficultand limited feature.I had some ruby committers saidI actually want to remove this featureor not.I'm so sad.Refinement is pretty cool.I like this feature.There is another use case.It'ssuper private method.This defined methodused in only this fileabsolutely.It's useful forrefactoring,for example,methodextraction.And second,I want to talk about method hooks.Anybody know method hooks?If you have used it before,please rise up your hands.Thank you.I think that it is not popular feature.These hook methods are calledwhen a method is defined or removedor undefined.For example,this sample codeshows how method update we have.Method update is calledfrom the line of death pool.In this case,line number 7and receives thedefined method name as symbol.Method hook provides a wayto implement method modifierlike public,private,protected.By the way,I made three method modifiers.Final modifier.Final 4-bit method override.Second,override modifier.Override enforce that target method hassuper method.Third,abstract modifier.Abstract enforce that target methodis overridden.These modifiers is similar withones of Java.If you know Java,please imagine these modifiersand you can understand easily.These method modifierswork when Ruby defines class.It is runtime,but in mostcase,this checking is donebefore main application logic.In other words,there is no overhead.Here is a sample code of final stream.Final stream provides a final modifier.When a method is definedor when a class includes a moduleor extends a module,ifsuper class has final method and the method is overrided,exception is occurred.Like this.This is the sample of overriderjam.When class definition is finished,if modified method has nosuper method,exception is occurred.This is the sample of overriderjam.When a class definition is finished,themodified method is not overridden,exception is occurred.How to implement these method modifiers?I used so many hook methods and included extended method addedand trance point,and also I usedZipper.In other words,I used the power of many blackmagics in Ruby.Here isuse case of method addedin finalist.Method added is main method verification logic.Method hook is useful forimplementing method modifiers,butI cannot implementfinal modifieronly by method hook.Why?Ruby has so many cases of method definition and they or define method includes moduleextend prepend.So,Inch case calls dedicated hooks.Why?Becauseinclude changes onlyChain of method lookup.When Ruby programming includes the module,Ruby interface inserts moduleto a hierarchy of method lookup.It's different from method adding.It's important.For your information,class ancestors method displays classand module hierarchy.For this reason,Finalist Jam uses many hooksin order to cover various cases.Method addedin to detectoverlided by subclass.Single-tone method addedinto detect overlided subclass.Single-tone classinclude it and extend it like so.And,I want to talk aboutTresspoint too.Overlider Jamand Absraker Jam useTresspoint.In this Jam,inherited hook or included hook startsTresspoint tracing.Tresspoint hook,I checkclass module hierarchy todetect method existence.So,module instance methodthe method is very useful for such a situation.Because method class has super method method.Super method method provides a way to trace method lookup chain.I'm using the word method too many timesand maybe you are confusing.Why did I use Tresspoint?Luby defines method dynamically.It's determined at runtime.Because of it,I must waituntil the end of class definitionto know a method is present or not.Overlider and Absrakercannot detect violationjust when they are called.In Ruby,the only wayto detect violation,detect such violationisTresspoint.And,I have advanced use caseof Tresspoint in Absraker Jamand Overlider Jam.Must detectfinish of specific class definitionin Tresspoint hook.In such situation,there is one pointthat needs attention.Luby has two syntax to define class.Class end statementand class.new with code block.Class.new with block isjust method code.It's important.In other words,end eventof Tresspoint cannot detectclass.new with code block.Because of this,I usedsee return event and return value property.It may cause trouble.I was in heavy trouble.I have another advanced case.It's Tresspoint and Ripper combination.Do you know Ripper?Ripper is a built-in librarybut not popular.So,standard library.Ripper is a parser for Ruby code.It can output token list and S expression.S expression representsconstruction of Ruby code.It has token swings and token positions.It is similar to ASD.By the way,at the future option,we can use ruby vmcolon abstract syntax tree module.It's already added in current ruby trunk.It has better interfacethan Ripper.It's great work.Sample code of Ripperis this.This code is from ruby reference manual.There is a token position in nested array.Liter of at identityand m string m.So that nested array is token position.This is the use case of abstract gene.I'm sorry,this sample is very complicated.Please see detailsafter we upload this deck.Events of threadpoint has file path and line numberand S expressionthat is output by Ripperhas token position.I can detect constructionof code syntax easily by this information.In abstract geneI check construction of Ruby code to detectwhether some method modifier is called in class definition or out ofclass definition.Like this,Ripper empowers threadpoint.Threadpoint detects events and where it occursand Ripper.sexpmethod provides how methods are called.We can get detailed informationabout importing methodsby this information.One of the other use casesPowerSortGem is implementedby this combination.PowerSortGem is built in gem for testing.Anyway,these gemsare proof of conceptbut these are decent practical.I think thatblack magic is dangerous actuallybut it is very funand it extends Ruby potentialso we can change Ruby syntaxby Ruby code itself.Because of these featuresI like Ruby very much.We have one questionRuby quiz.What is the difference betweenundef method and remove method?I pass the mic to Morison during thinking time.Did you get answer?When we call the instance methodwho,the Ruby virtual machinesuch as the Ruby machineRuby virtual machine and we will find the definitionof method who as the class bar.The class bar is the subclassof the superclass whoand also who also has a definitionof method who but anywaymethod who in class bar will be called.When we callremove method methodon the class bar,it justremove the definitionof the method whoand from class bar.When we call the instance method whoRuby virtual machinewill dig the method lookup chainand then we will find themethod definition of who from classbar and when we do theundef method whoon class barthat method masks the definitionof method whoon class barand marks that method do not exist.When we callthe instance method whoon class barRuby virtual machine raises no method error.When you want toreplace no method error,you should use theundef method.Anyway,this ismy use caseof these method programming techniques.When we willwrite middleware orhuge applications,wewill do the manytimes of resource allocationsand then we willresource releases andresource means huge memories orfiles,firehundreds,sockets,data-based connectionsand many others.So we need to releasethe resources when we will not use the resource anymore.But thenof course we can use and beginand insure our clauses for that methodand but then that is a bit complexand messyand theother languages havespecial idioms for that purposes.For example,thisso Java codeshows that try with resources crowdsand for that purpose,only for that purpose.But then that is very simple and usefulto allocate resourcesand then release these resourcessafely in safe way.So of course we will havea way to handle these problems.That is an open methodwith blocks.The open methodof file class takeargument of paths and theninbrockthe file will be opened and thenat the end ofbrock,the filewill be closed.But thenthat is a kind of loopy way,I think.But thenit requires many indentations.One indentationspowerless source and then sometimesthat method is not implemented.And forexample,TCP method,TCP socketsthis type of method.So thatI create I load a RubyGemwith resources.ThisRubyGem is torealize this safe resource allocationand using top-level with methodby kind of refinement.So this with methodit takes just one argumentof proc object to assign resourcesand then it passesassigned resources tothe block parameter.So interesting thingis this argument blockhave two statements.Of courseit can haveany lines or any number of statementsbutthat's proc object having two statementsand just returns onevalue,but it cancatch the allocated resourcesand talk and db and thenand pass these values to block parameters.So that is,of course,andtrashpoint.It usestrashpoint and events be return and lineand to pass allocated resources toblock parametersand identifyand to identify allocated resourcesin lambda statement.And also it also usesbindingto detect newly defined local variablesin allocation lambda.And thenit also uses refinement totop-level with methodwithout any side-effects.But thisidium still requiresindentation for block.So that'sand when we need,we want toassign resources inmarch step,first time,second and third timeand it...if we callwith methods three times,it requiresthree-level nesting.So itis not so bad,but it looksa bit messy,so that'sand we want anymuch cool alternative.Andgo-rank haven't deferred.Defurreddo not require indentationsand also deferred method callwill be called at the end of context.That looks very smart.So yes,I created this.Sorry.So we have some option to implement deferredand an optionis to use deferred classand the kind of that.And alsoit's also takenbrock.And then in thatbrock we can use deferred processing.But it alsoit still requiresindentation.Andand after callingdefurred method,we canreplace the value of variableswhich uses in blocksand it releasesunexpected exceptionsor resource leakage.So that isnot safe.SoI created deferred or gemand of course usingrefinements and to introduceand top-level deferred method.Andso the sample code isdefurred.And thatisn't just look likevery similar to the deferredin Gorang,I think.Of courseI use TressPoint.Sodefurred method enables TressPoint if not yetand it initialized internal stockframes.Sodefurred manageswhen the resources should be releasedusing its own internal stockframeand TressPoint monitorsthe method called stack and getsand it gets a snapshotof local variables in deferred blockand it calls release blockat the end of scope.Scope meansthat's virtual stockframe.Andit also uses in binding to storeand restore the set of local variables.of release blockand it also usesrefinements to introducetop-level deferred method without any side-effects.That is ansorry.Andso these techniques arevery powerful and we can do many thingsusing these techniques,butthe hard thing is debugging.If we missedto disable or TressPoint worksat the expected time,the TressPoint will continuewill continue to workforever.Andthe only way to stop itis to sendthe signal to the Ruby process.That is a very hard thing.But also it providesa kind of superpower to usand to realize many good things,many interesting things.Sothat's a gentlemansaid long,long agofar away,give yourselfto the dark side.It is the only wayyou can save your friends.Thank you very much.