Created
February 26, 2019 23:40
-
-
Save fijiaaron/b0d0930c436f82116e0c1c1b7c20ae94 to your computer and use it in GitHub Desktop.
RubyAccessors created by fijiaaron - https://repl.it/@fijiaaron/RubyAccessors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo | |
# initialize instance variables | |
def initialize(bar="bar", baz="baz") | |
@bar = bar | |
@baz = baz | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo | |
# initialize instance variables | |
def initialize(bar="bar", baz="baz") | |
@bar = bar | |
@baz = baz | |
end | |
# generate default getters and setters | |
attr_accessor :bar, :baz | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo | |
# initialize instance variables | |
def initialize(bar="bar", baz="baz") | |
@bar = bar | |
@baz = baz | |
end | |
# generate default getters and setters | |
attr_accessor :bar, :baz | |
# add custom getter | |
def bar() | |
return @bar.capitalize() | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo | |
# initialize instance variables | |
def initialize(bar="bar", baz="baz") | |
@bar = bar | |
@baz = baz | |
end | |
# generate default getters and setters | |
attr_accessor :bar, :baz | |
# add custom getter | |
def bar() | |
return @bar.capitalize() | |
end | |
# add custom setter | |
def baz=() | |
@baz = @baz.reverse() | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative("test.rb") | |
# initialize class with private instance variables | |
class Foo | |
def initialize(bar="bar", baz="baz") | |
@bar = bar | |
@baz = baz | |
end | |
end | |
# create global foo to test | |
$foo = Foo.new() | |
test("no accessors") | |
# add default getters and setters | |
class Foo | |
attr_accessor :bar, :baz | |
end | |
test("attr_accessor") | |
# add custom getter | |
class Foo | |
def bar() | |
@bar.upcase() | |
end | |
end | |
test("custom getter") | |
# add custom setter | |
class Foo | |
def baz=(baz) | |
@baz = baz.reverse() | |
end | |
end | |
# set instance variable | |
$foo.baz = "baz" | |
test("custom setter") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def test(message) | |
begin | |
puts "Test with #{message}" | |
puts $foo.bar, $foo.baz | |
rescue StandardError => e | |
puts e.message | |
end | |
puts("\n") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment