Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6119515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:33:43+00:00 2026-05-23T15:33:43+00:00

I want this class to initialize to receive a message to be saved and

  • 0

I want this class to initialize to receive a message to be saved and enter a filename for it. Am I drawing up an error because Ruby only wants values to be instantiated in the init method? Be gentle, I’m new to this. Traceback pasted below.

class DobbsyKretts
  idea = 'arbitaryvalue'
  def initialize
    #Receive idea
    puts "Enter an idea, a secret or anything else you want to encrypt. Hit enter to stop typing and save the file"
    @idea.gets.reverse.upcase
    #Filename and saving - to encrypt the file.
    puts "Enter the file name you'd like to have this saved as. Type PLAN at the beginning for plans and REM for reminders"
    @file_name.gets.strip
    File::open("DobbsyKrett-"+ file_name + ".txt", "w") do |f|
      f>>@idea
    end
  end
end

something = DobbsyKretts.new

Traceback:

testy.rb:11:in `initialize': private method `gets' called for nil:NilClass (NoMethodError)
    from testy.rb:21:in `new'
    from testy.rb:21:in `<main>'
Enter an idea, a secret or anything else you want to encrypt. Hit enter to stop typing and save the file
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:33:44+00:00Added an answer on May 23, 2026 at 3:33 pm

    You are calling getson @idea before having assigned a value – that’s one of the reasons why you get the error. Also, gets should not be called on the instance variable here. Try it like this:

    class DobbsyKretts
      def initialize
        #Receive idea
        puts "Enter an idea, a secret or anything else you want to encrypt. Hit enter to stop typing and save the file"
        (@idea = gets).reverse.upcase
        #Filename and saving - to encrypt the file.
        puts "Enter the file name you'd like to have this saved as. Type PLAN at the beginning for plans and REM for reminders"
        @file_name = gets.strip
        File::open("DobbsyKrett-"+ @file_name + ".txt", "w") do |f|
          f << @idea
        end
      end
    end
    
    something = DobbsyKretts.new
    

    This works as you expected, but I just would like to remind you that it is a very bad idea to do something like this in the constructor. You should rather use a dedicated method for generating files and/or asking the user for input.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to convert something like this: class NestedItem attr_accessor :key, :children def initialize(key,
class DobbsyKretts def initialize #Receive idea puts Enter an idea, a secret or anything
In Ruby, I have this class: class Position attr_reader :x, :y def initialize(x, y)
I want to serialize this class: public class CarDisplay { public string Name {
What I want is this behavior: class a: list = [] x = a()
With Django models, I want to achieve this: class Foo(models.Model): name = models.CharField(max_length=50) #wrapping
Given this class class Foo { // Want to find _bar with reflection [SomeAttribute]
given this class definition: public class Frame { IFrameStream CapturedFrom; } I want implement
I want to do something like this: class Cls { function fun($php) { return
I have subclassed a UITableViewCell and within this class I want to get it's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.