ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello, im currently/trying to learn some basic programming as i start college next year, and i'm just trying to get a bit of a head start by building some small applications. I'm trying to get the function printSomething inside of a class to print "hello world".
Code:
#!/usr/bin/ruby
class Test
def printSomething
puts "hello world"
end
end
printSomething #this doesnt work
But if i remove the class the function prints "hello world". Is there a way to print something inside a class. Sorry, terrrible at this.
Thanks for your help
I'm not a Ruby programmer, but some things tend to be applicable across languages.
Usually, with methods in a class, you have to either call them on an instance of the class, or qualify them using the class name. How else would you differentiate similarly named methods in different classes, for example? So, you'd do something like Test.printSomething() (again, please remember that I'm not a Ruby programmer, so I'm unfamiliar with the syntax)
Also, functions tend to be defined and called with parentheses at the end. Otherwise, how would you differentiate them from variables? Actually, just looking at some Ruby docs, this doesn't seem to be a problem in that language.
I'd suggest following a Ruby tutorial, or getting a book where the examples are explained. You might want to take a look at "Ruby in Twenty Minutes", listed on the Ruby docs page: https://www.ruby-lang.org/en/documentation/. On pages 2 and 3, I saw an example of what you're trying to do.
I am with the others that you should really look at one of the 1000's of tutorials out there and learn the basics before jumping ahead like you have, otherwise, when advised to create an instance
of a class it will mean virtually nothing.
If you are going to continue along the Ruby line (which I would recommend as I think it is a great language), I would bookmark the following:
http://www.ruby-doc.org/core - whilst ri gets you all the doco at the command line, I find this invaluable as a quick click around resource
https://github.com/bbatsov/ruby-style-guide - this one will take some time to work through, but it does provide some great advice on best ways to display your code (ruby specific mainly) and would cover
off on some of the points mentioned in post #2 on how Ruby likes it
In pretty much every language out there, "classes" are intended to be used to define "objects," which are instantiated and then "told what to do." ("Hey, you! Do that!") An object is simply a block of memory, where the local variables of the object are kept, along with the necessary housekeeping information to allow the runtime system to recognize that it is an object, what kind (class ...) of object it is, what methods and properties it supports, and how to invoke or obtain them.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.