Constructor (Java) ---> initialize (Ruby)

Like constructors in Java, I understood that in Ruby, we use
“initialize” for this issue.

If I have the following in Java:

public class ReadFile
{
public static short[][] setFile(fileName)
{
ReadFile rf = new ReadFile();
}
}

Regarding:

public static short[][] setFile(fileName)

In a previous thread, I understood that I can represent it in ruby as:

def set_file(filename)
end

But, now, as for:

ReadFile rf = new ReadFile();

Does the preceding Java code look like the follows in Ruby:

class ReadFile
def set_file(filename)
def initialize
end
end
end

Thanks.

Thanks @Markus.

Wat do you think @Brian?

On 20.09.2010 11:59, Abder-Rahman A. wrote:

Does the preceding Java code look like the follows in Ruby:

class ReadFile
def set_file(filename)
def initialize
end
end
end

More like:

class ReadFile
def set_file=(filename)
end
end

and use it like

reader = ReadFile.new
reader.set_file = “filename”

You haven’t defined an explicit constructor (source wise). Java creates
implicit ones, I’m not sure about ruby. Let Brian answer that :slight_smile:

  • Markus

Abder-Rahman A. wrote:

class ReadFile
def set_file(filename)
def initialize
end
end
end

No. You can’t nest defs (or at least, they won’t do what you expect).

I don’t grok Java, so I’m not sure exactly what you want. It looks like
setFile is creating a new ReadFile object, so is setFile actually a
class method, not an instance method?

class ReadFile

# This is a class method

def self.set_file(filename)
  new(filename)              # short for: self.new(filename)
end

# These are instance methods

def initialize(filename)
  @filename = filename
end

def read
  File.read(@filename)
end

end

If this is true, I’d say “set_file” is a poor name for what is basically
a factory method. In most cases such factory methods aren’t used; the
user of this class would just create an instance directly. e.g.

rf = ReadFile.new("/etc/motd")
puts rf.read

But if I’ve misunderstood, and you want set_file to be an instance
method, then it could look like this:

class ReadFile

def initialize(filename)
  set_file(filename)
end

def set_file(filename)
  @filename = filename
end

def read
  File.read(@filename)
end

end

Or:

class ReadFile

def initialize(filename)
  self.file = filename
end

def file=(filename)
  @filename = filename
end

def read
  File.read(@filename)
end

end

Or you could use attr_accessor. This writes accessor methods for you, in
this case ‘filename’ and ‘filename=’ to read and write @filename
respectively.

class ReadFile

attr_accessor :file

end

Example

rf = ReadFile.new
rf.file = “/etc/motd”

Markus F. wrote:

You haven’t defined an explicit constructor (source wise). Java creates
implicit ones, I’m not sure about ruby. Let Brian answer that :slight_smile:

There is an implicit constructor: it calls ‘allocate’ to create the
object, and then ‘initialize’ to fill in its instance variables. The
default initialize, inherited from Object, does nothing.

Examples:

class Foo
end
f = Foo.new
puts f.inspect # f has no instance variables

class Foo
def initialize
@iv = 123
end
end
g = Foo.new
puts g.inspect # g has one instance variable

class Foo
def initialize(val = 123)
@iv = val
end
end
h = Foo.new # has @iv=123
i = Foo.new(456) # has @iv=456

Note how we can modify the class even after we’ve created existing
objects from it :slight_smile:

A good starting point for self-study is:
http://www.ruby-doc.org/docs/ProgrammingRuby/

HTH,

Brian.

On Mon, Sep 20, 2010 at 11:59 AM, Abder-Rahman A.
[email protected] wrote:

}
}

That seems pretty weird Java code. A method named “setFile” with a
single argument is conventionally a setter and does not return
anything. However, it seems you intend to do much more here. I
assume you are aiming for something like

public static short[][] readFile(File fileName) throws IOException {
final FileReader fr = new FileReader();
fr.setFileName(fileName);
fr.readFile();
return fr.getData();
}

ReadFile rf = new ReadFile();

Java: new X();
=>
Ruby: X.new

Does the preceding Java code look like the follows in Ruby:

class ReadFile
def set_file(filename)
def initialize
end
end
end

No, as others have explained already. There are some differences
between Ruby and Java. First and foremost Ruby does not have
overloading of method names with different argument lists so you
always ever have at most one #initialize per class and per module. If
you want to differentiate according to arguments you need to do it
yourself.

Then, in Java “new” is an operator while in Ruby it’s just an instance
method of class Class:

irb(main):001:0> Class.instance_method :new
=> #<UnboundMethod: Class#new>

You usually never fiddle with method #new but all you do is to provide
an implementation of #initialize. But in Ruby you can actually take
control over object allocation which is out of your control in Java.
For example, you can implement a singleton pattern like this:

irb(main):002:0> class Foo
irb(main):003:1> INSTANCE = allocate
irb(main):004:1> def self.new; INSTANCE; end
irb(main):005:1> end
=> nil
irb(main):006:0> f1 = Foo.new
=> #Foo:0x101a8484
irb(main):007:0> f2 = Foo.new
=> #Foo:0x101a8484
irb(main):008:0> [f1,f2].map {|f| Foo::INSTANCE.equal? f}
=> [true, true]
irb(main):009:0>

Note: you do not need to do this as there is module Singleton already
which does the job for you - although a bit differently.

Kind regards

robert