词汇:file

n. 文件;档案;锉刀;文件夹

相关场景

48.memoization优化模式:
def current_user
@current_user ||= User.find(logged_in_user_id);
def shipped?(order_id) @status ||= begin file = fetch_confirmation_file ; file? parse_confirmation_file(file) : {} end @status[order_id] == :shipped end
返回的是true or false; 而current_user 返回的是变异对象;如果不希望调用者修改缓存的变量,那应该考虑让被记忆化的方法返回冻结对象。
>> Effective Ruby
24. block and ensure:
分配-使用-释放;file = File.open(file_name, 'w'_...file.close;因为又垃圾回收的原因,中途发生异常也会被回收资源,但是你不确定什么时候会被回收,所以你如果能及时处理,那更好。那么就算需要begin。。。ensure file.close if file end;也可以通过 File.open(file_name, 'w') do |file| ...end,这种block的方式。块执行晚后文件会被关闭。将资源管理简单化。 简单化的资源管理,也衍生出一个 传block给方法的问题,可以用class Lock def self.acquire ; lock =new ; lock.exclusive_lock!; if block_given? yield(lock) else lock # Act more like Lock::new. end ensure if block_given? # ...end end end; 没有绑定块时候,Lock::acquire 。。。; 类方法上使用块和ensure语句将资源管理的逻辑抽离出来。
>> Effective Ruby
5.ambiguous 语义不清的问题:
'808'.split(/0/); dirs = ['usr','local']; File.join(*dirs); dirs.map(&:length);$VERBOSE=true or -w or RUBYOPT=-w 会显示提示;
>> Effective Ruby
We're not gonna change that for this little example, but now let's check in that PWA files, and then let's deploy to production one more time. and as you can see, look in the top right corner, when I reload it, we now have that little install icon in Chrome. and if I click that little install icon, I'm gonna get this prompt, and boom! I have a PWA running in production for my Rails application.
>> Rails 8.0.1 You are in good company
We're gonna turn the manifest on in our route file as well. There are basically two lines here as you can see, there's a manifest, and there's a service worker that you can use for your PWA.
>> Rails 8.0.1 You are in good company
we're gonna create a link reference here to manifest just exists here as a comment that you can reference. We're gonna turn the manifest on in our route file as well.
>> Rails 8.0.1 You are in good company
So that's just gonna be my email address and 123 password!We hop back into our CLI, and run rails db:seed, that's gonna run that file , I just showed you and set things up.
>> Rails 8.0.1 You are in good company
If we go back here and reload not in production, boom! We are live in production with our whole setup, everything is working, we can upload the active storage files directly to it.
>> Rails 8.0.1 You are in good company
There was a 404 here! That's because if we go back to out route file, I have not defined route! And in production, you're not gonna get that screen we saw with the Rails version and the Ruby version, that is only for development.
>> Rails 8.0.1 You are in good company
And that'll connect to the remote server, and it'll install Docker if it's missing,it'll build the Docker file or Docker container off the Docker file that Rails ship with by default, there's nothing you need to set up there.
这将连接到远程服务器,如果缺少Docker,它将安装Docker,它会根据Rails默认附带的Docker文件构建Docker文件或Docker容器,你不需要在那里设置任何东西。
>> Rails 8.0.1 You are in good company
Docker Hub:
And there's a default configuration file in config/deploy.yml that we can use, it's prefilled a little bit, it has the service name of the name of reaction, but we need to rename, for example, your user,the name of the image to go to my name of where I store this on Docker Hub. You can see we change that down in the registry as well and the name of the container image.
>> Rails 8.0.1 You are in good company
So if we scroll down to the bottom here, we are ready. The first thing we're gonna do, we're gonna add a turbo stream from post to the show files to the show template.That's gonna set up the web socket connection and subscribe us to a channel named after that particular post that's pasted in.
>> Rails 8.0.1 You are in good company
It'll automatically know that the comment model should map to view slash comment slash comment, so it can look up the right partial file to use. And then below that, we have the form that we're referencing with the comments new. So, let's hop in and paste in the individual comment. As you can see here, we just give it a div, that has a dom ID so that we can reference it.
>> Rails 8.0.1 You are in good company
But we can also have a look at a specific file, you'll see it matches exactly what we have back there. That's not a development setup! That is what we're shipping in production. There is no minification, there's no transpilation, there's none of that nonsense because you just don't need it.
>> Rails 8.0.1 You are in good company
inspector:
But really, what's unique here for Rails is the fact that we're using no build by default! So if I go over here in the inspector and look at the JavaScript files that are included,you can see we have the application js file with a little digest stamp on there. If we change anything that application js file, the digest is going to change, and the browser will redownload just the part!
>> Rails 8.0.1 You are in good company
But now that we've add that, let's have a look at our application JS file.That's the default setup that you have that the scaffold is going to use. And as you can see, we're using turbo-rails, we're including all the stimulus controllers, if we have any.
>> Rails 8.0.1 You are in good company
digesting:
You don't need anything beyond what Rails ship you with already, because Rails 8 is all no build by default! That means there's not a transpiler, that means there's not a bundler, these files are shipped directly to the browser over HTTP2, so it's nice and fast. And the import map is what allows us to refer to these files by their logical names while still doing far future digesting, so that they load really quick,and such that they're easily compatible with CDNs and all that good stuff.
除了Rails已经提供的内容之外,你不需要任何东西,因为Rails 8默认情况下都是无构建的!这意味着没有转译器,也就是说没有打包器,这些文件通过HTTP2直接发送到浏览器,所以它既好又快。导入映射允许我们通过这些文件的逻辑名称引用它们,同时仍在进行未来的消化,这样它们加载得非常快,并且很容易与CDN和所有好东西兼容。
>> Rails 8.0.1 You are in good company
As you can see here, there is now a full WYSIWYG interface for creating the body. It comes with a default set of styles for the toolbar, you can change those, those styles are generated straight into your application, so you can make it look nice for yourself. Let's give some bold and italic text here, you see, that was all that was needed. but I think what's even nicer to look at here is if we do an upload and we add a file, you will see that file gets added with a preview directly to the WYSIWYG editor. And if we save that and we update the post, it is added to the post itself. And that then went through the whole process of doing a direct upload of the file when we dropped it into the editor, that uploads it straight to active storage. And then, we have access to that, and rendering it directly from whatever storage backend active storage is using. In this example , we're just storing on disk, but you could be storing your active storage in S3 or another object storage.
>> Rails 8.0.1 You are in good company
Active Storage is a way to deal with attachments and other files in your Rails application. When you run it through action_text:install, it'll automatically set up those active storage tables that we need, there is one for blob,and then we have one for text here.We run migrations to set that up again, and now that we've run action_text:install, it also added a couple of gems, so we need to restart our development server. I do that just by exiting out and just running the server again!
>> Rails 8.0.1 You are in good company
compile:
By default, I like to use simple CSS. https://cdn.simplecss.org/simple.css. It is simply a individual file that's being included, I don't have to compile anything here, I'm just referencing it straight off their CDN. And if we save that and reload, you can see it just looks a little nicer.
>> Rails 8.0.1 You are in good company
That post is just going to have a title that's string and a body that's a text, and as you can see here from what's being generated, we have everything that we need to set up a basic interface for that scaffold. There is a migration that'll set things up in the database. There is a controller, there are views, there's a model, there's even testing stubs and adjacent API on top. So let's run that migration, and as you can see here, we created the posts in the main schema file, and now, we're ready to have a look at the application that was generated here with the post scaffold.
>> Rails 8.0.1 You are in good company
(beat) I don't know a lot, Will. But let me tell you one thing. All this history, this sh*t... (indicates file) Look here, son.
>> Good Will Hunting (1997)Movie Script
SEAN:
Oh, this is your file. I have to send it back to the Judge with my evaluation.
>> Good Will Hunting (1997)Movie Script
Sean smiles. A beat. Will sees a FILE on Sean's desk.
>> Good Will Hunting (1997)Movie Script
Will takes a seat next to Chuckie and they crack open their beers. Other workers file out of the site. They drink.
>> Good Will Hunting (1997)Movie Script