Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    gak
    gak is offline
    New Coder
    Join Date
    Dec 2006
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question New to Ruby from Java

    Hi

    I'm new to Ruby but have some experience in Java. To try and get to grips with Ruby I've been playing around with it and trying to make some old classes I wrote in Java to work in Ruby.

    This is a bare-bones class I wrote in Java:

    Code:
    public class Helper {
      //attributes
      private String name;
      private String address;
      private String telephone; 
        
      public Helper(String name, String address, String telephone) {
        //constuctor 
        this.name = name;
        this.address = address;
        this.telephone = telephone;  
          }
    //Followed by appropriate getter/setters and methods/main() etc.
    This is what I done so far in Ruby and seems to work:


    Code:
    class Helper
    attr_accessor :name, :address, :telephone
    
    def initialize (name, address, telephone)
    @name = name
    @address = address
    @telephone = telephone
    end
    
    end
    The part that is confusing me is making the attributes private as in my Java code, and then doing the suitable getter/setter methods to go with it. I'm not sure how to lay the code out?

    If anyone could offer some advice or point me in the direction of a good newbie guide, I'd be much abliged!

    Many thanks

    Gak

  • #2
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    I don't know of any newbie guides, but I can explain the getter/setter stuff.

    Instance variables are private by default in Ruby, so all you have to do to make the class in your example equivalent to the Java class is remove the attr_accessor line.

    attr_accessor, along with attr_reader and attr_writer are shortcuts for the simplest possible getter and setter methods:

    Code:
    attr_reader :name
    # (read public) equivalent to =>
    # def name
    # 	return @name
    # end
    attr_writer :address
    # (write public) equivalent to =>
    # def address=(address)
    # 	@address = address
    # end
    attr_accessor :telephone
    # (read and write public) equivalent to a combination of attr_reader 
    # and attr_writer
    If you want to provide your own getter or setter to provide additional functionality or to make virtual attributes or whatever, simply take out the shortcut, or change it to provide only the other type of accessor, and write your own:

    Code:
    # Methods that end in = are lvalue eligible, so you can do
    # class_instance.telephone = 1231234
    # to call this
    def telephone=(telephone)
    	raise Exception.new('Yeah right') if telephone == 5551212
    	@telephone = telephone
    end
    
    # allow classinstance.address
    def address
    	return @address.upcase
    end
    Last edited by ralph l mayo; 08-19-2007 at 01:51 AM.

  • Users who have thanked ralph l mayo for this post:

    gak (09-08-2007)

  • #3
    gak
    gak is offline
    New Coder
    Join Date
    Dec 2006
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thank you very much for your help - much appreciated indeed!!!

    I wish the 'Beginning Ruby' book I'm reading had explained things as well as that

  • #4
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    You're welcome. I hope you're enjoying Ruby, or at least that you will after you get over the steep part of the learning curve.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •