Utility of to_s

nio kasgami

VampCat
Veteran
Joined
May 21, 2013
Messages
8,949
Reaction score
3,042
First Language
French
Primarily Uses
RMMV
Hi dear people

I work a lot on some script and I notice the people use a lot 

to_s

I never really understand in begin but now I understand ;3

the use of to_s 

serve for convert numbers or float in Strings 

I guessed it is a lot use in Graphics stuff for enumerate the graphics (index purpose most of time)

def somethingself = Sprite.newself.bitmap = Cache.system("com_" + index.to_s)endit will convert the ''index'' who are a number in  String 

if you don't do the to_s it will Send you a error  who can't convert number and float in strings

but with this it pass the restriction and allow you to convert it

so it is all I can explain about to_s

see ya people ;3
 

cremnophobia

Veteran
Veteran
Joined
Dec 10, 2013
Messages
216
Reaction score
97
Primarily Uses
Don't do that, please. Use string interpolation instead:

Code:
"com_#{index}"
It's faster and uses less memory. It's also easier to read (for me and many other people).
 
Last edited by a moderator:

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
First Language
English
Primarily Uses
RMMV
I use to_s a bit (but usually only when outputting stuff to the console that I'm going to delete anyway).


I've seen the method above, but haven't looked at it enough to understand it properly. It does seem to be a really tidy way to do it, and used in Ace quite a bit, so I SHOULD learn about it.


I've never seen the format command before. It looks just like sprintf. Any differences?
 

nio kasgami

VampCat
Veteran
Joined
May 21, 2013
Messages
8,949
Reaction score
3,042
First Language
French
Primarily Uses
RMMV
Don't do that, please. Use string interpolation instead:

"com_#{index}"It's faster and uses less memory. It's also easier to read (for me and many other people).
HOO thanks I never knew this method when I think it seem more logic to use!

I use to_s a bit (but usually only when outputting stuff to the console that I'm going to delete anyway).

I've seen the method above, but haven't looked at it enough to understand it properly. It does seem to be a really tidy way to do it, and used in Ace quite a bit, so I SHOULD learn about it.

I've never seen the format command before. It looks just like sprintf. Any differences?
Well I am less Acoutumated to Sprintf I only use it for Print Error Message in my system unless that I don't use it a lot so I can't answer correctly the answer :(
 

cremnophobia

Veteran
Veteran
Joined
Dec 10, 2013
Messages
216
Reaction score
97
Primarily Uses
I've never seen the format command before. It looks just like sprintf. Any differences?
They're equal, so there shouldn't be any differences.
Code:
method(:format) == method(:sprintf)  # => true
I guess the first method name is a bit clearer for everyone. The second isn't. I mean who knows the s means string and f means formatted? Ruby (and other languages) just has inherited some C features (including 017 which isn't 17).
 

Napoleon

Veteran
Veteran
Joined
Dec 29, 2012
Messages
869
Reaction score
97
First Language
Dutch
Primarily Uses
.to_s is nothing but a method. I believe it comes from the Object class.

You can create or even override to_s with:

# NOT recommended for live projects!class Fixnum  def to_s    return 'hi!'  endendString interpolation and many other methods probably also call .to_s() internally.

to_s() converts WAY more than just fixnums and floats. It simply depends on the class that it is invoked on. My own classes sometimes override the to_s method.

And yes string interpolation is better than

"string1" + x.to_s + "string 2"because it reads clearer (imo) and it is slightly faster.
 

whitesphere

Veteran
Veteran
Joined
Mar 14, 2014
Messages
1,688
Reaction score
784
First Language
English
Whenever I create a custom class for scripting, I almost always add a to_s method which helps me immensely for debugging..  It is just a method which should return something that can convert to a string, like this:

define to_s

     return "Hello"

end

Or

define to_s

  result=""

  result += @current_x.to_s+" "+@current_y.to_s

 return result

end
 

nio kasgami

VampCat
Veteran
Joined
May 21, 2013
Messages
8,949
Reaction score
3,042
First Language
French
Primarily Uses
RMMV
Wow.....I never thinked it was so much way for define the to_s 

myself I only use the to_s for Transform Float or digit stuff in String 

so I never overwrite it 

but thanks I learn so many new things : D

method:)format) == method:)sprintf)  # => true 

(I guess method is something I don't use but I guess method is like definition?)
 

whitesphere

Veteran
Veteran
Joined
Mar 14, 2014
Messages
1,688
Reaction score
784
First Language
English
I recommend downloading Ruby itself (it's free) and working through the many excellent examples.  It comes with a free PDF that is a very good introduction to Ruby, with examples.

You can download it here:  https://www.ruby-lang.org/en/downloads
 

nio kasgami

VampCat
Veteran
Joined
May 21, 2013
Messages
8,949
Reaction score
3,042
First Language
French
Primarily Uses
RMMV
I recommend downloading Ruby itself (it's free) and working through the many excellent examples.  It comes with a free PDF that is a very good introduction to Ruby, with examples.

You can download it here:  https://www.ruby-lang.org/en/downloads
Ho I have some good basis in ruby just I can't know everythings xD 

but thanks you ;3
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??

Forum statistics

Threads
105,862
Messages
1,017,049
Members
137,570
Latest member
fgfhdfg
Top