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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:31:29+00:00 2026-05-28T07:31:29+00:00

I am a newbie to Ruby and am learning it using Programming Ruby 1.9

  • 0

I am a newbie to Ruby and am learning it using “Programming Ruby 1.9” (PickAxe). In the book, I see a program which I modified thus:

 1 #!/usr/bin/env ruby -w
 2 
 3 class BookInStock
 4         attr_reader :isbn
 5         attr_accessor :price
 6         def initialize(isbn, price)
 7                 @isbn = isbn
 8                 @price = Float(price)
 9         end
10 #       def price=(price)
11 #               @price = Float(price)
12 #       end
13 end
14 
15 b1 = BookInStock.new("isbn1", 3)
16 p b1
17 b2 = BookInStock.new("isbn2", 3.14)
18 p b2
19 b3 = BookInStock.new("isbn3", "5.67")
20 p b3
21 b3.price = "10.32"
22 p b3

Line number 8 ensures that the right value is assigned to b3.price.

But how do I handle cases like line number 21 without using a method like in line 10-12?

Is there some way in which I can modify the attr_accessor for this? Or am I asking for too much 😀

I could not find any such references online.

  • 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-28T07:31:29+00:00Added an answer on May 28, 2026 at 7:31 am

    attr_accessor :sym is a method of a Class that defines simple getter and setter methods.

    You can define your own casting_attr_accessor:

    class Class
    
     def casting_attr_accessor(accessor, type)
    
         define_method(accessor) do  
           instance_variable_get("@#{accessor}")
         end        
    
         define_method("#{accessor}=") do |val| 
           instance_variable_set("@#{accessor}", Kernel.send(type.to_s, val))
         end
     end
    end
    

    And then in your class use it like

    casting_attr_accessor :price, Float
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a newbie to Rails and programming in general (currently learning from a book
Is is that I'm a newbie learning Ruby, or does it really have more
I'm definitely a newbie to ruby (and using 1.9.1), so any help is appreciated.
Newbie here...can I write one program which incorporates .NET LINQ and also various Java
I'm a Ruby on Rails newbie. I'm learning Rails in my spare time (my
I'm just learning Ruby so apologies if this is too newbie for around here,
I've just started learning Ruby on Rails and it's a newbie question. I did
Newbie to ruby this is my first project I am using the FasterCSV Gem
Apologies -- I'm a newbie using Ruby on Rails. Still a little confused about
Programming newbie here, about a month in with Ruby. I'm currently playing around with

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.