How to call a class from another script?

How to call a class from another script?

On Fri, Feb 25, 2011 at 6:16 PM, Anthony Ob [email protected]
wrote:

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!

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

What if i didnt use def?

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?

On Fri, Feb 25, 2011 at 8:53 PM, Anthony Ob [email protected]
wrote:

and I want to add the name class from this script:

puts “Hello, what is your name?”

name = gets.chomp

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 6:50 PM, Anthony Ob [email protected]
wrote:

What if i didnt use def?

That dog wouldn’t bark :slight_smile:

Maybe you should ask a more specific question, or run through a
basic tutorial on Ruby first.

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

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
## now you “call a class from another script…”
namegetter = NameGetter.new
## and get the “name” attribute
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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs