LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Ruby, call a function inside class (https://www.linuxquestions.org/questions/programming-9/ruby-call-a-function-inside-class-4175522020/)

sampleLInux 10-13-2014 08:43 PM

Ruby, call a function inside class
 
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

ndc85430 10-14-2014 12:33 AM

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.

a4z 10-14-2014 12:45 AM

you need an instance of Test

Test.new().printSomething

grail 10-14-2014 10:08 AM

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 :)

sundialsvcs 10-15-2014 09:23 PM

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.


All times are GMT -5. The time now is 06:56 AM.