Easy A
Would you like to react to this message? Create an account in a few clicks or log in to continue.


 
HomeHome  Latest imagesLatest images  SearchSearch  RegisterRegister  Log inLog in  

Share
 

 New to Ruby from Java

View previous topic View next topic Go down 
AuthorMessage
Database
Member
Member
Database

Posts : 318
Join date : 2012-03-04

New to Ruby from Java Empty
PostSubject: New to Ruby from Java   New to Ruby from Java EmptyFri Jul 27, 2012 10:57 pm

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
Back to top Go down
Database
Member
Member
Database

Posts : 318
Join date : 2012-03-04

New to Ruby from Java Empty
PostSubject: Re: New to Ruby from Java   New to Ruby from Java EmptyFri Jul 27, 2012 10:58 pm

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
Back to top Go down
Database
Member
Member
Database

Posts : 318
Join date : 2012-03-04

New to Ruby from Java Empty
PostSubject: Re: New to Ruby from Java   New to Ruby from Java EmptyFri Jul 27, 2012 10:58 pm

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
Back to top Go down
Database
Member
Member
Database

Posts : 318
Join date : 2012-03-04

New to Ruby from Java Empty
PostSubject: Re: New to Ruby from Java   New to Ruby from Java EmptyFri Jul 27, 2012 10:59 pm

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.
Back to top Go down
Sponsored content




New to Ruby from Java Empty
PostSubject: Re: New to Ruby from Java   New to Ruby from Java Empty

Back to top Go down
 

New to Ruby from Java

View previous topic View next topic Back to top 
Page 1 of 1

 Similar topics

-
» Ruby Java Bridge
» Java and Databases
» [java] Share a variable?
» looking for a specific java script code
» Bad Little Java Thinker - Recaptcha Faking

Permissions in this forum:You cannot reply to topics in this forum
Easy A :: New to Ruby from Java Edit-trash Useless :: Trash-