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

Crystal function gets not waiting for user input
How to parse data from POST request
Read a single char from stdin without pressing enter
What object receives #puts in my Crystal programs?
Avoid repeating variable name in assignation
Sharing variables between different macros
passing json to a superclass using .from_json
execution of command failed with code: 1: `cc -o "/root/.cache/crystal/var-app-staging-new-http.cr
How to use union in “if” statement [Crystal]
How to convert a hex string to bytes array in Crystal?
is INSTANCE = new in class is run after compilation automatically or the first time it is accessed?
Crystal lang: understanding method return type of Void
Crystal-lang httpget basic_auth
How to control using CPU cores [Crystal]
How can I read a slice with other size in crystal?
Unix-style password readline

Categories

HOME
cygwin
gridview
design-patterns
seo
database-normalization
spring-data-mongodb
banner
discrete-mathematics
accessibility
angular-dart
gmm
boost-icl
blade
definitelytyped
fable-f#
underscore.js
angular-translate
winexe
bioinformatics
uitapgesturerecognizer
constexpr
mustache
rselenium
laravel-eloquent
biztalk-rule-engine
hibernate-search
arduino-ide
batching
apic
jpad
filesystemwatcher
loading
apriori
install.packages
logback-groovy
smoothstate.js
android-checkbox
flask-login
opencmis
phpdbg
proget
robomongo
configurationmanager
static-cast
compiler-design
subnet
sundials
mouseleave
template-toolkit
cmp
polyline
usart
pytables
iotivity
tinkerpop3
burrows-wheeler-transform
greatest-n-per-group
lz77
pushbots
ascii-art
canalyzer
haskell-warp
android-broadcast
entry-point
tinyxml
mt4j
eclipse-kepler
notorm
mongohq
cffile
diem
apache-abdera
intersect
garbage
usertype
role
catransform3d
drawtobitmap
rowlex
hbm2java
cfwindow
web-application-project
business-model
contentpresenter
movieplayer
great-circle

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App