Ruby Class error output [closed] - ruby

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
can anyone tell me why the last line of my code output nothing? THX

You need to use initialize not init method,
class Mystuff
def initialize
#apple="I am instance"
end
def talk
puts 'I am taking'
end
attr_reader :apple
end
thing = Mystuff.new
thing.talk
puts thing.apple
output
I am taking
I am instance

Related

Method cannot be called inside of a class [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
This program runs as intended if outside a class, how could this be run inside of one?
class Pointester
$points = 0
def point_tracker()
puts $points
end
point_tracker()
end
Here's the error: undefined method `point_tracker' for Pointester:Class (NoMethodError)
Change it to:
class Pointester
$points = 0
def point_tracker
puts $points
end
end
Pointester.new.point_tracker
And you might want to consider using an instance instead of a global variable – depending on your use-case.

Why can interactive Ruby run a program, but sublime editor not? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I wrote code like this:
class Animal
attr_accessor :name, :color, :age
end
first_animal = animal.new
first_animal.name = "Floyd"
first_animal.color = "white"
first_animal.age = 7
puts first_animal.name
When I save it and drag onto "command prompt with Ruby" and press enter, it says undefined local variable, but when I write the code using interactive ruby (IRB terminal) it works fine. It shows me the results of first_animal.name etc. Where is the problem?
You have syntax mistake:
Animal.new instead of animal.new

.sample on an array is always false in irb [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am testing in irb and everytime I require my file is is always returning false.
class Weather
def stormy
[false, true].sample
end
end
it seems to return false know matter what is in the array?
I am unsure if it is my code or the way I am requiring file in irb.
can anyone help
Thank you
To pick randomly from an Array use sample:
def stormy
[ false, true ].sample
end
The return value of require (or require_relative) indicates whether the file was loaded successfully or was already loaded. It has nothing to do with any methods that may be defined inside the file.
Once the file defining it has been loaded, to call your method (as currently defined) you would do
Weather.new.stormy

Why wont this print "Welcome to Ruby!" [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm following the Ruby course on Codeacademy and it's asking me to create a method, named welcome, that puts "Welcome to Ruby!" After defining the method, call it. Here's my code:
def welcome()
puts "Welcome to Ruby!"
end
Why doesn't this work?
You need to actually call the method after:
def welcome
puts "Welcome to Ruby!"
end
# Call it
welcome
What you've done so far is create a function that does something, but you're never calling that function. Functions and methods don't do anything by themselves until called / invoked.
I don't know what this tutorial you're following entails but more than likely you're missing that invocation line, which can probably go at the end of the file that contains this function:
# your function definition here...
welcome()

Should I use File.write? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I recently run into problems converting a ruby script to .EXE because I had a File.write statement in it. The documentation doesn't mention the write method but when I do a
pp File.methods
it is there. So should I use File.write? In a normal Ruby script the following works
File.write("test.txt", "test")
But is it good practice and why doesn't the documentation mention it?
File.write is in fact IO.write (File is a child of IO) which can be verified by monkey-patching:
class IO
def IO.write
puts "IO's class method write was called."
end
end
File.write # outputs "IO's class method write was called."
It is very well in the documentation.
Thus, I see no reason not to use it.

Resources