Programming Ruby: The Pragmatic Programmers' Guide
Author:David Thomas; Chad Fowler; Andrew Hunt
Language: eng
Format: mobi
Tags: Non-fiction, COMPUTERS / Programming Languages / Ruby, Reference
ISBN: 9780974514055
Publisher: Pragmatic Bookshelf
Published: 2000-12-15T10:00:00+00:00
# this can be mixed in but the variable x won't be visible
def no_bar
return x
end
def bar
@x = 1000
return @x
end
puts( "In Foo: x = #{x}" ) # this can access the module-local x
end
include Foo # mix in the Foo module
When you run this program, the puts method executes when the module is initialized, and it displays the value of the module-local variable x:
In Foo: x = 50
If you display the x variable within the main scope of the program, the value of the variable x local to the main scope of the program is used, not the value of the variable x local to the module:
puts(x) #=> 1
But any attempt to execute the no_bar method will fail:
puts( no_bar ) # Error: undefined local variable or method 'x'
Here the no_bar method is unable to access either of the local variables named x even though x is declared both in the scope of the module (x = 50) and in the current or “main” scope (x = 1). But there is no such problem with instance variables. The bar method is able to return the value of the instance variable @x:
puts(bar) #=> 1000
A module may have its own instance variables that belong exclusively to the module “object.” These instance variables will be in scope to a module method:
inst_class_vars.rb
module X
@instvar = "X's @instvar"
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Coding Theory | Localization |
Logic | Object-Oriented Design |
Performance Optimization | Quality Control |
Reengineering | Robohelp |
Software Development | Software Reuse |
Structured Design | Testing |
Tools | UML |
Deep Learning with Python by François Chollet(12555)
Hello! Python by Anthony Briggs(9904)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9785)
The Mikado Method by Ola Ellnestam Daniel Brolund(9769)
Dependency Injection in .NET by Mark Seemann(9329)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8282)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7756)
Grails in Action by Glen Smith Peter Ledbrook(7686)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7550)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(7007)
Microservices with Go by Alexander Shuiskov(6774)
Practical Design Patterns for Java Developers by Miroslav Wengner(6684)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6629)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6399)
Angular Projects - Third Edition by Aristeidis Bampakos(6035)
The Art of Crafting User Stories by The Art of Crafting User Stories(5566)
NetSuite for Consultants - Second Edition by Peter Ries(5497)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5302)
Kotlin in Action by Dmitry Jemerov(5048)
