TAILIEUCHUNG - Rails for Java Developers phần 3

Phiên bản ổn địn mới nhất là . Ruby có nhiều thay đổi đáng kể so với dòng . Ví như: Biến cục bộ trong khối lệnh Thêm cú pháp khai báo hàm nặc danh (fun = -(a,b) { puts a + b }) Chọn kiểu mã hóa ký tự cho từng chuỗi Socket API mới (hỗ trợ IPv6) | Defining Classes 61 Note that some of the accessor method calls are prefixed with an object reference other. and other accessor calls are not. Methods not prefixed by an explicit object reference are invoked on this the object whose method is currently executing. Here s the Ruby marry def marry other - end since Ruby methods always return the last expression evaluated writer setter methods return the new value set. This means that multiple setters can be chained together as in . Note that the Ruby setter methods being called in marry are prefixed by an object either other or self. Ruby s self is the equivalent of Java s this. The explicit use of self here is important. In Ruby last_name X is ambiguous. Depending on the context this might mean Create a local variable named last_name with value X or Call the method last_name passing the parameter X . Using self makes it clear that you want the method interpretation. if you forget to prefix a setter with self you may create hard-to-find bugs. Java does not suffer from this ambiguity so be careful. Creating Static Methods sometimes methods apply to a class as a whole instead of to any par - ticular instance of a class. In Java these are called static methods static methods public static String getSpecies return Homo sapiens In Ruby these methods are called class methods class methods def Homo sapiens end For purposes of this book we can pretend that Java static methods and Ruby class methods are similar 2. The Ruby story is actually a good bit more complex than this. Unlike Java methods Ruby class methods are polymorphic. There are at least five alternate syntaxes that you can use to define a Ruby class method. There is a good RubyGarden discussion on class methods at http 3000 Ruby page show ClassMethods. In addition Ruby also has class variables but we think you should avoid them. see

TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã 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.