Go Back   CodingForums.com > :: Server side development > Ruby & Ruby On Rails

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-18-2007, 02:40 PM   PM User | #1
gak
New Coder

 
Join Date: Dec 2006
Posts: 20
Thanks: 3
Thanked 0 Times in 0 Posts
gak is an unknown quantity at this point
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
gak is offline   Reply With Quote
Old 08-19-2007, 12:40 AM   PM User | #2
ralph l mayo
Regular Coder

 
ralph l mayo's Avatar
 
Join Date: Nov 2005
Posts: 951
Thanks: 1
Thanked 31 Times in 29 Posts
ralph l mayo is on a distinguished road
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 12:51 AM..
ralph l mayo is offline   Reply With Quote
Users who have thanked ralph l mayo for this post:
gak (09-08-2007)
Old 08-19-2007, 12:30 PM   PM User | #3
gak
New Coder

 
Join Date: Dec 2006
Posts: 20
Thanks: 3
Thanked 0 Times in 0 Posts
gak is an unknown quantity at this point
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
gak is offline   Reply With Quote
Old 08-19-2007, 11:24 PM   PM User | #4
ralph l mayo
Regular Coder

 
ralph l mayo's Avatar
 
Join Date: Nov 2005
Posts: 951
Thanks: 1
Thanked 31 Times in 29 Posts
ralph l mayo is on a distinguished road
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.
ralph l mayo is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:49 AM.


Advertisement
Log in to turn off these ads.