crystal-lang


Get instance property instead of undefined method


class Place
#description = "Default place"
def initialize(x : Int32, y : Int32, description : String)
#x = x
#y = y
#description = description
puts "Description of this place is: #{description}"
end
end
require "./browser-game/*"
require "./places/*"
module Browser::Game
# TODO Put your code here
place = Place.new 2, 3, "Yay new description"
puts place.description
puts "End of the program"
end
I receive this error:
Error in browser-game.cr:8: undefined method 'description' for Place
puts place.description
^~~~~~~~~~~
Write this:
class Place
getter :description
#description = "Default place"
def initialize(x : Int32, y : Int32, description : String)
#x = x
#y = y
#description = description
puts "Description of this place is: #{description}"
end
end
getter is used to give access to the read property from an instance. setter is for setting. Without this, the compiler will try to access the method since you did not give access to the property.

Related Links

Embedded Crystal variables in templating
Crystal: a good way for method options
Crystal: abstract static method
Type checking in Crystal
Condition over object's type
Crystal C bindings: argument const unsigned char **
Crystal C bindings: return type char*
Make Class member attribute of type Array(T) accept 2D arrays of T
JSON.mapping if “root” attribute has inherit keys
Crystal C bindings: using fopen
Define Class object validations
Set attribute of type “Class” to define it as Int32, String, Float64
Set both default attribute and type of attribute
How can I test private methods?
Write binary data to stdout in Crystal
Unable to compile after upgrade from crystal 0.19.4 to 0.20

Categories

HOME
fabricjs
turing-machines
c99
jax-rs
pda
datastore
ns-3
jmx
youtube-data-api-v3
yahoo-finance
frontend
webix
nfs
endeca
android-securityexception
alamofire
code-generation
symbol
scsi
package.json
atlassian-stash
mapstruct
cfml
simpleitk
microsoft-ocr
guzzle
google-api-client
flooding
nstimer
tizen-tv
eviews
scatter3d
dst
mustache
r-grid
spring-security4
jett
biztalk-rule-engine
pdw-file-browser
imx6
video-capture
tex
c9.io
wmp
flask-restful
linter
wcftestclient
reportservice2010
context-switch
backbone-collections
mathcad
shinobi
change-tracking
vcloud-director-rest-api
neon
wso2developerstudio
ng-grid
dup
delta
eigenvector
pushbots
design-principles
sciruby
iokit
vigenere
doctype
visual-sourcesafe-2005
slash
vacuum
baasbox
liquid-layout
fail2ban
mass-assignment
paw
strawberry-perl
template-deduction
jjaql
memset
twitter-bootstrap-rails
amslidemenu
http-patch
jeromq
oocss
time.h
joomla-template
cascadingdropdown
redis-py
mkv
midlet
vfs
luabind
pinch
filemerge
u2netdk
google-ajax-api
quotation-marks
http-daemon
codebase
interprocess
eclipse-marketplace
hbm2java
glui

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App