..


Sponsored Links

How do I create a class in Ruby?

A class defines a schema with which to represent an object, each object has methods and attributes, respectively, that is, actions and information with which the object works.

A simple example for creating a class in Ruby can be the following:






 # Assignment of a name to the class







 class Greeting



  



 attr_reader: destination



  



 # Definition of the method



  



 def initialize (address)



   



 # Definition of the variable



    



 @ Recipient = recipient



  



 end



  



 final outcome



    



 # Action Method





    



 puts "Hello # {recipient}"



  



 end







 end

 







 # Instance of the class







 Greeting Saluto.new = ("MrWebmaster")







 # Method call







 saluto.esito



The output produced is as follows:
 



 Hello MrWebmaster

 

In the same category ...
E-Learning
Ruby and Ruby on Rails (Course) Ruby and Ruby on Rails (Course)
Create software and Web applications with Ruby and RoR. From 39 €.
Sponsored Links