How to call a class from another script?
···
--
Posted via http://www.ruby-forum.com/.
How to call a class from another script?
ripple:/tmp$ cat > dog.rb
class Dog
def bark
puts "woof"
end
end
ripple:/tmp$ irb
load 'dog.rb'
=> true
dog = Dog.new
=> #<Dog:0x102296c58>
dog.bark
woof
=> nil
HTH!
On Fri, Feb 25, 2011 at 6:16 PM, Anthony Ob <vidgametester@gmail.com> wrote:
--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
twitter: @hassan
Well I got this script:
puts "Do you feel, good, bad, or neutral?"
feelings = gets.chomp
STDOUT.flush
puts "You are feeling " + feelings
and I want to add the name class from this script:
puts "Hello, what is your name?"
name = gets.chomp
STDOUT.flush
puts "Hello, " + name
How would I do that?
--
Posted via http://www.ruby-forum.com/.
hello anthony,
first, i completely agree with hassan that a basic ruby tutorial is
absolutely the best place to start. there are many very good ones
available - do a quick google search for "ruby tutorial."
second, if i understand your question - try this:
#############
class NameGetter
attr_reader :name
def initialize
puts "Hello, what's your name?"
@name = gets.chomp
puts "Hello, " + @name
end
end #class
class NameAndFeelingGetter
def initialize
namegetter = NameGetter.new
name = namegetter.name
puts "How do you feel?"
feelings = gets.chomp
puts "Well, #{name}, sounds like you're feeling #{feelings}"
end
end #class
nfg = NameAndFeelingGetter.new
###############
then run the program...
happy ruby-ing...
-j
--
Posted via http://www.ruby-forum.com/.
ripple:/tmp$ cat > dog.rb
class Dog
def bark
puts "woof"
end
end
ripple:/tmp$ irbload 'dog.rb'
=> true
dog = Dog.new
=> #<Dog:0x102296c58>
dog.bark
What if i didnt use def?
--
Posted via http://www.ruby-forum.com/\.
If that's the whole "script", 'name' is a variable, not a class.
Again -- find and work through a tutorial on basic Ruby.
On Fri, Feb 25, 2011 at 8:53 PM, Anthony Ob <vidgametester@gmail.com> wrote:
and I want to add the name class from this script:
puts "Hello, what is your name?"
name = gets\.chomp
--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
twitter: @hassan
That dog wouldn't bark
Maybe you should ask a more specific question, or run through a
basic tutorial on Ruby first.
On Fri, Feb 25, 2011 at 6:50 PM, Anthony Ob <vidgametester@gmail.com> wrote:
What if i didnt use def?
--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
twitter: @hassan
truth that if he look like he want swat him on the nozzles
yes it lite trouble but there lab, can automatic every command dang. thats make too of us. bouble dang, save a lot work uh auto speaker u think that the directing. awersome we are save. p.s hi susan
Date: Sat, 26 Feb 2011 13:39:36 +0900
From: hassan.schroeder@gmail.com
Subject: Re: How to call a class from another script?
To: ruby-talk@ruby-lang.orgOn Fri, Feb 25, 2011 at 6:50 PM, Anthony Ob <vidgametester@gmail.com> wrote:
> What if i didnt use def?
That dog wouldn't bark
Maybe you should ask a more specific question, or run through a
basic tutorial on Ruby first.--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
twitter: @hassan