All language subtitles for pragstudio-ruby-blocks-08-block_initializer (Transcribed on 27-Apr-2023 21-06-55)

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
ny Chichewa
zh-CN Chinese (Simplified) Download
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:00,000 --> 00:00:08,720 We're not going to look at another block pattern that turns up in a very unexpected place. 2 00:00:08,720 --> 00:00:13,440 If you've ever written a Ruby gem, then you might have seen something like this. 3 00:00:13,440 --> 00:00:17,760 So we're creating a Ruby gem specification here using the specification class that's 4 00:00:17,760 --> 00:00:22,680 inside of the gem module, but notice we're calling new to initialize a new object, 5 00:00:22,680 --> 00:00:25,240 but the new method takes a block here. 6 00:00:25,240 --> 00:00:29,799 The block parameter is S, that's the specification object that's being initialized or 7 00:00:29,799 --> 00:00:34,760 created, and then inside of the block, we can just assign values to some of the attributes 8 00:00:34,760 --> 00:00:36,320 of the specification. 9 00:00:36,320 --> 00:00:40,320 In this particular case, I've all signed the result of all this to the spec object, 10 00:00:40,320 --> 00:00:42,000 and then printed it out down below. 11 00:00:42,000 --> 00:00:47,599 And if we run this, well, the 2S method in the gem spec class, it just prints out the name, 12 00:00:47,599 --> 00:00:52,200 in this case it'd be my gem, and the version is 2.0.0, which is what we set up in the 13 00:00:52,200 --> 00:00:53,200 block. 14 00:00:53,200 --> 00:00:59,800 And the block, in this case, gives us a way to set up that object inside of a block structure 15 00:00:59,800 --> 00:01:00,800 here. 16 00:01:00,800 --> 00:01:01,800 So how does this work? 17 00:01:01,800 --> 00:01:04,879 Well, let's try writing our own shorter version of this. 18 00:01:04,879 --> 00:01:08,640 I'm going to abbreviate this a little bit because we don't need all these attributes, 19 00:01:08,640 --> 00:01:09,640 right? 20 00:01:09,640 --> 00:01:13,440 And because gem, the gem module, on the specification class, they've already been written, 21 00:01:13,440 --> 00:01:16,400 and they're actually loaded in the Ruby programs, I'm going to change this around. 22 00:01:16,400 --> 00:01:21,840 Our modules are going to be called gem with a j, and that way we won't run into any collisions 23 00:01:21,840 --> 00:01:25,040 with the built-in specification class in RubyJems. 24 00:01:25,040 --> 00:01:26,640 All right, let's go ahead and implement this. 25 00:01:26,640 --> 00:01:27,640 We'll do it right up here. 26 00:01:27,640 --> 00:01:32,240 We're going to have a module, it's called gem with a j, inside of there, we're going to have 27 00:01:32,240 --> 00:01:37,120 a class called specification. 28 00:01:37,120 --> 00:01:38,680 It has some attributes. 29 00:01:38,680 --> 00:01:40,640 We'll have extra accessors for those. 30 00:01:40,640 --> 00:01:45,720 We have name, version, and summary. 31 00:01:45,720 --> 00:01:48,520 And then we have an initialized method. 32 00:01:48,520 --> 00:01:53,679 Now remember, when you call new and Ruby to create a new object, new allocates the memory, 33 00:01:53,679 --> 00:01:57,320 but then behind the scenes that turns around and calls the initialized method to actually 34 00:01:57,320 --> 00:01:59,920 initialize that object. 35 00:01:59,920 --> 00:02:02,800 So in the initialized method, we generally set attributes. 36 00:02:02,800 --> 00:02:07,520 In this case, we can set the version attribute to be by default 1.0.0. 37 00:02:07,520 --> 00:02:08,519 All right? 38 00:02:08,519 --> 00:02:14,840 And you might do more default initialization right here, as you would, any initialized method. 39 00:02:14,840 --> 00:02:19,120 But we know that this new method, we passed the block to that, and it could do some more 40 00:02:19,120 --> 00:02:20,120 initialization. 41 00:02:20,120 --> 00:02:21,560 So how do we pull that off? 42 00:02:21,560 --> 00:02:25,720 Well, inside of our initialized method here, we just yield to the block. 43 00:02:25,720 --> 00:02:29,840 We need to give it the object that's being created, which is the specification object 44 00:02:29,840 --> 00:02:31,160 in this case. 45 00:02:31,160 --> 00:02:37,400 We can get that by using self, inside of initialize, self is the object we're initializing. 46 00:02:37,400 --> 00:02:40,760 And we only want to do that if a block was given. 47 00:02:40,760 --> 00:02:43,200 So we use block underscore given, question mark there. 48 00:02:43,200 --> 00:02:45,959 All right, we save that and run it. 49 00:02:45,959 --> 00:02:48,280 Well now it prints out our gems specification. 50 00:02:48,280 --> 00:02:49,959 I want to see what the attributes are in there. 51 00:02:49,959 --> 00:02:53,440 So when it changes a little bit, I'm going to use our old friend P right there to print 52 00:02:53,440 --> 00:02:55,079 out its native form. 53 00:02:55,079 --> 00:02:59,640 And we see we've got a gems specification object whose version is 2.0. 54 00:02:59,640 --> 00:03:05,000 Notice it's not 1.0 as up in here, because our block overwrote that version number 55 00:03:05,000 --> 00:03:06,000 right here. 56 00:03:06,000 --> 00:03:08,519 So our block did further initialization. 57 00:03:08,520 --> 00:03:14,000 This name is my gem and the summary is, this is a cool gem. 58 00:03:14,000 --> 00:03:19,760 So this block based style makes it very clear that inside of the block, all the statements 59 00:03:19,760 --> 00:03:25,160 in that block are focusing on initializing this same object. 60 00:03:25,160 --> 00:03:27,560 So what if we don't pass it up block? 61 00:03:27,560 --> 00:03:29,120 Well we're checking if a block is given. 62 00:03:29,120 --> 00:03:30,200 So we should be okay there. 63 00:03:30,200 --> 00:03:33,680 If we take this block and I'm just going to comment that out right there. 64 00:03:33,680 --> 00:03:35,320 So we don't have it associated. 65 00:03:35,320 --> 00:03:39,320 And then we can initialize attributes as we normally were with an object say I'm going 66 00:03:39,320 --> 00:03:43,040 to set the name of our gem to be something like Mike. 67 00:03:43,040 --> 00:03:45,720 Well that should still work and it does. 68 00:03:45,720 --> 00:03:51,320 But notice that the version now is 1.0.0 because that's the default version and the name 69 00:03:51,320 --> 00:03:52,320 is now Mike. 70 00:03:52,320 --> 00:03:57,320 So because we use the block given, we can associate a block or not associate a block. 71 00:03:57,320 --> 00:04:01,840 So whether you want to use a block or not is really a stylistic design decision. 72 00:04:01,840 --> 00:04:06,120 You can assign attributes as you normally would like this or if you want to make it very 73 00:04:06,120 --> 00:04:12,160 clear that this chunk of code is all focused on initializing the object then you can 74 00:04:12,160 --> 00:04:13,960 pass a block in there. 75 00:04:13,960 --> 00:04:19,079 So when I was researching code for this course I was surprised how often I found this block 76 00:04:19,079 --> 00:04:24,400 initialization pattern in the wild and you'll see a lot more examples of this in the exercise. 77 00:04:24,400 --> 00:04:28,120 In the next section we're going to look at the final use for blocks and it's a great way 78 00:04:28,120 --> 00:04:33,120 to manage resources. 7292

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.