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 7652643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:49:32+00:00 2026-05-31T11:49:32+00:00

I have a class class Person attr_accessor :name,:age def initialize(name,age) @name = name @age

  • 0

I have a class

class Person
    attr_accessor :name,:age
    def initialize(name,age)
        @name = name
        @age = age
    end
end

I’d like to make the age optional so its 0 if its not passed, or the name to be blank if not passed

Ive researched a bit on it but its a bit confusing as to what i’ve found (having to pass variables in another variable { }).

  • 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-31T11:49:33+00:00Added an answer on May 31, 2026 at 11:49 am

    It’s as simple as this:

    class Person
        attr_accessor :name, :age
    
        def initialize(name = '', age = 0)
            self.name = name
            self.age = age
        end
    end
    
    
    Person.new('Ivan', 20)
    Person.new('Ivan')
    

    However, if you want to pass only age, the call would look pretty ugly, because you have to supply blank string for name anyway:

    Person.new('', 20)
    

    To avoid this, there’s an idiomatic way in Ruby world: options parameter.

    class Person
        attr_accessor :name, :age
    
        def initialize(options = {})
            self.name = options[:name] || ''
            self.age = options[:age] || 0
        end
    end
    
    Person.new(name: 'Ivan', age: 20)
    Person.new(age: 20)
    Person.new(name: 'Ivan')
    

    You can put some required parameters first, and shove all the optional ones into options.

    Edit

    It seems that Ruby 2.0 will support real named arguments.

    def example(foo: 0, bar: 1, grill: "pork chops")
      puts "foo is #{foo}, bar is #{bar}, and grill is #{grill}"
    end
    
    # Note that -foo is omitted and -grill precedes -bar
    example(grill: "lamb kebab", bar: 3.14)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class like this, public class person { name Name {set; get;}
For example I have a simple class like public class Person{ public int Age
I have class Person as following : class Person { char* name; int age;
I have a class:Person class Person{ String name; String age; } I want to
I have a class Person and a class Name. Name contains two Strings firstName
Say I have my class @interface Person : NSObject { NSString *name; } I
Say I have a .NET class like so: public class Person { public string
For example I have class to serialize [Serializable] class Person { [XmlAttribute(name)] string Name
I'm working with Sinatra and RSpec. I have this in lib/auth.rb class Person attr_accessor
Suppose i have class Person { public int Id {get;set;} public string Name {get;set;}

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.