Ruby for Rails phần 5

Để biễu diễn một số thực dấu chấm động, ta phải theo qui tắc dùng kí số zero () hay chuyển đổi tường minh (). Việc dùng dấu chấm là không đủ (99.) vì các số vẫn có thể nhận cú pháp có phương thức. Thiếu một kiểu dữ liệu kí tự ("char"). Điều này có thể gây ngạc nhiên khi duyệt qua chuỗi: "abc"[0] cho ra 97 (một số nguyên, biểu diễn mã ASCII của kí tự đầu tiên trong chuỗi); để lấy được "a" dùng "abc"[0,1] (chuỗi con có chiều dài 1) hay "abc"[0].chr | 176 CHAPTER 6 Modules and program organization When you create your application there s nothing in the models directory you create all the models with the generate script. However you get a free controller file the generic file app controllers which serves as an umbrella controller file for all the other controllers. Upon the automatic creation of this file you see something similar to what you saw in the newly minted model files class Applicationcontroller ActionController Base end This code creates a class or perhaps reopens a class you can t tell by looking although in this case the action is creation called ApplicationController which is a subclass of a class called Base that is nested inside a class or module module as it happens called ActionController. The new class created here ends up serving as the superclass for the other controller classes you create later with the generate script as you can see if you look in one of the application-specific controller files class Composercontroller Applicationcontroller We ll come back and flesh out the ramifications of this discussion particularly in part 4 when we revisit the music store application and bear down on further details of coding inside the model and controller files. Meanwhile you now have a good sense of the centrality of modules as well as classes which again are a specialized form of module to Ruby programming in general and the Rails framework specifically. Summary This chapter has been both a companion to and a continuation of the previous chapter on classes. We ve looked in detail at modules which are similar to classes in that they bundle methods and constants together but which can t be instantiated. You ve seen examples of how you might use modules to express the design of a program. We ve taken an object s-eye view of the process of finding and executing a method in response to a message. We ve also looked at some techniques you can use including nesting classes and modules .

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.