Are there primitive types in Ruby?
Asked Answered
M

4

27

I'm a Java developer who is just starting to learn Ruby. Does Ruby have any primitive types? I can't seem to find a list of them. If not, why?

Mathur answered 13/9, 2013 at 15:50 Comment(1)
I know this doesn't answer your question but if you're not familiar with Ruby, there is a great tutorial on Codecademy at (www.codecademy.com/tracks/ruby). Hopefully by going through the tutorials you can save yourself a lot of troubleshooting time down the road!Traynor
G
40

A core principle of Ruby is that all data should be represented as objects. Other languages such as Smalltalk follow a similar paradigm.

The benefit of this design is that it makes Ruby more elegant and easier to learn. The rules applying to objects are consistently applied to all of Ruby.

For instance, when beginners are first learning Java, the difference between the primitive type int and the wrapper class Integer can be confusing. This confusion is exacerbated by the sometimes confusing implicit conversions between the two via autoboxing.

So why would languages like Java or C# bother with primitive types? The answer is performance. Creating objects incurs additional overhead when compared with primitives.

Gentlemanly answered 13/9, 2013 at 15:52 Comment(6)
OMG! within 1 minute or less you wrote these many lines... :)Twinflower
Even what other languages consider "primitives" are objects in Ruby. For example: 5.object_id is different from 5902123.object_id. These are two different Fixnum objects.Hillari
@VirtualDXS I'm not sure what Ruby you're using but on mine it returns the same value consistently.Hillari
@Hillari You're correct; I'm mistaken. Strings behave like that, so I assumed fixnums would too.Sheilahshekel
Strings absolutely do, that's why Ruby has the Symbol construct instead. Comparing equivalence is easy if the object_id matches. If not, comparing strings is much more time consuming.Hillari
@Gentlemanly even on C# under the hood primitive types is objectsBituminize
T
20

There are no primitive data types in Ruby. Every value is an object, even literals are turned into objects:

    nil.class  #=> NilClass
   true.class  #=> TrueClass
  'foo'.class  #=> String
   :bar.class  #=> Symbol
    100.class  #=> Integer
   0x1a.class  #=> Integer
0b11010.class  #=> Integer
  123.4.class  #=> Float
1.234e2.class  #=> Float

This allows you to write beautiful code like:

3.times do
  puts "Hello from Ruby"
end
Testy answered 13/9, 2013 at 17:8 Comment(2)
thx for your answer. is there a list of all primitive objects in ruby? e.g. Hash, Integer, Float etc.Hellbent
@Hellbent objects than can be crated using a special syntax are documented under literals. Maybe have a look at Ruby’s core library.Testy
L
6

Quoting from About Ruby

In Ruby, everything is an object. Every bit of information and code can be given their own properties and actions.

In many languages, numbers and other primitive types are not objects. Ruby follows the influence of the Smalltalk language by giving methods and instance variables to all of its types. This eases one’s use of Ruby, since rules applying to objects apply to all of Ruby.

Java chooses to preserve some primitive types mainly for performance, but you have to admit, not every type is a class does makes Java code a little awkward sometimes. The philosophy of Ruby is to make the programmer's days easier, I think making everything an object is one way to achieve this.

Latt answered 13/9, 2013 at 15:54 Comment(2)
is => an object? Strange that in Ruby I can do 1.method(:+).class and get Method, but {}.method(:=>).class is a syntax error. It's almost as though... not everything is Objects?Electrosurgery
Well => is not a method on Hash. But {}.method(:[]).class returns Method, as you'd expect. So no, not everything in Ruby is an object, if you include in "everything" odd bits of syntax, whitespace, reserved words, etc. -- that's expecting a bit much.Rationalize
V
3

There are no primitive data types in ruby. Because ruby is a purely object-oriented language. Basically, there are data types like other languages, but these data types are classes like collections in java.

If you define any string value like "Akshay" then it is an object. You can check the below image in which "Akshay" has object_id 30300. Please click on the link to check the objects on the rails console. From more examples from the image, we can decide that everything is an object in ruby excluding keywords.

So here we can conclude ruby converted these primitive data types into classes.

Ruby console having primitive data as object

Varityper answered 12/2, 2021 at 12:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.