In the above example login_as() and get() are method calls. Both take symbols as parameters
admin and :index) and by taking advantage of Ruby's flexibility (no need to parenthesise method arguments in a lot of cases) and the ability to join two statements together using "and", you get the above.
To do your example in ruby you would simply do:
Code:
this, that = nil, 0
There is no need to declare instance variables as private as for all intents and purposes Ruby instance variables are always private (that is, you have to declare a getter/setter for an instance variable if you need read or write access). You can do that in two ways, using attr_reader, attr_writer and attr_accessor macros or manually if you need to do something more advanced.
For example, if you have an object foo with an instance variable bar, you could do:
Code:
class FooClass
attr_reader :bar
# or for write access...
# attr_writer :bar
#
# or for both...
# attr_accessor :bar
def initialize
@bar = 'hello'
end
end
attr_reader would create a getter method for @bar called bar(), attr_writer would create a setter method for @bar called bar=() and attr_accessor does both. Of course you could define these yourself:
Code:
class FooClass
def initialize
@bar = 'hello'
end
def bar()
@bar
end
def bar=(attr)
@bar = attr
end
end
Either way you would use them as follows:
Code:
foo = FooClass.new
puts foo.bar #=> 'hello'
foo.bar = 'world'
puts foo.bar #=> 'world'
Note that in the above, doing:
Code:
foo.bar = 'something'
is really just syntatic sugar and is the equivalent of the following:
Code:
foo.bar=('something')
Bookmarks