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
Utility of to_s
● ARCHIVED · READ-ONLY
-
-
Don't do that, please. Use string interpolation instead:
Code:It's faster and uses less memory. It's also easier to read (for me and many other people)."com_#{index}" -
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? -
HOO thanks I never knew this method when I think it seem more logic to use!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).
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 :(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? -
They're equal, so there shouldn't be any differences.I've never seen the format command before. It looks just like sprintf. Any differences?
Code: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).method(:format) == method(:sprintf) # => true -
.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. -
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 -
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?) -
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 xDI 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
but thanks you ;3