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

**URL:** <https://www.ruby-forum.com/t/constructor-java-initialize-ruby/195233>\
**Category:** Ruby\
**Created:** [September 20, 2010, 11:59am UTC](https://www.ruby-forum.com/t/constructor-java-initialize-ruby/195233 "2010-09-20T11:59:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [September 20, 2010, 11:59am UTC](https://www.ruby-forum.com/t/constructor-java-initialize-ruby/195233/1 "2010-09-20T11:59:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [September 20, 2010, 12:43pm UTC](https://www.ruby-forum.com/t/constructor-java-initialize-ruby/195233/2 "2010-09-20T12:43:14Z")

</div>

Thanks @Markus.

Wat do you think @Brian?

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [September 20, 2010, 12:39pm UTC](https://www.ruby-forum.com/t/constructor-java-initialize-ruby/195233/3 "2010-09-20T12:39:15Z")

</div>

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 🙂

- Markus

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [September 20, 2010, 12:45pm UTC](https://www.ruby-forum.com/t/constructor-java-initialize-ruby/195233/4 "2010-09-20T12:45:38Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [September 20, 2010, 12:50pm UTC](https://www.ruby-forum.com/t/constructor-java-initialize-ruby/195233/5 "2010-09-20T12:50:19Z")

</div>

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 🙂

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 🙂

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

HTH,

Brian.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [September 20, 2010, 1:20pm UTC](https://www.ruby-forum.com/t/constructor-java-initialize-ruby/195233/6 "2010-09-20T13:20:14Z")

</div>

On Mon, Sep 20, 2010 at 11:59 AM, Abder-Rahman A.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
