LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-13-2014, 08:43 PM   #1
sampleLInux
LQ Newbie
 
Registered: Oct 2014
Posts: 1

Rep: Reputation: Disabled
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
 
Old 10-14-2014, 12:33 AM   #2
ndc85430
Member
 
Registered: Apr 2014
Distribution: Slackware
Posts: 92

Rep: Reputation: Disabled
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.
 
Old 10-14-2014, 12:45 AM   #3
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
you need an instance of Test

Test.new().printSomething
 
Old 10-14-2014, 10:08 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
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
 
Old 10-15-2014, 09:23 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,671
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to call function inside c shared objects from a php extensions eilaf Linux - Server 1 10-21-2008 02:00 AM
how to call a function inside switch case sarathius Programming 4 01-28-2008 05:36 AM
Derived class inside parent class array, possible? xemous Programming 3 10-17-2006 11:35 AM
Defining a non-inline function inside class definition (tm) Programming 6 05-11-2005 09:14 AM
problem on dynamic function call inside a class! antony_csf Programming 0 06-29-2004 10:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration