Favorite scripting features?

Started by Seox, June 17, 2009, 09:17:29 pm

Previous topic - Next topic

Seox

June 17, 2009, 09:17:29 pm Last Edit: June 17, 2009, 09:32:33 pm by Seox
So, what're some of your favorite features/constructs in ruby?

As simple as it is, I use case statements A LAWT, and the include? method of class Array. I also tend to like the stategy of going:

frog = array.include?('urine')
frog = frog + ETC.....



Where you define over the SAME temporary variable in order to save space, memory, organization, and stuff.



What are some of yours?
... (<<<<<<<<<<<<<<< TEH DOTS OF DOOM. Hey, kinda catchy. :naughty:)

G_G

June 17, 2009, 09:40:16 pm #1 Last Edit: June 17, 2009, 09:43:40 pm by game_guy
I like the features of using methods in modules so I can easily make it where instead of using long lines of code like this
$game_variables[5] += $points
I can shorten it up using this
Gv.addval(5, $points)

I made a module and added methods in it so it shortens up code to make it not so long.

EDIT:
And I do it for other things. Use this
module A
  def self.ls(id, iid)
    $game_actors[id].learn_skill(iid)
  end
end


Then use A.ls(actor_id, skill_id)
what this does is instead of using
$game_actors[actor id].learn_skill(skill id)
you can use this instead
A.ls(actor id, skill id)

Seox

Quote from: game_guy on June 17, 2009, 09:40:16 pm
I like the features of using methods in modules so I can easily make it where instead of using long lines of code like this
$game_variables[5] += $points
I can shorten it up using this
Gv.addval(5, $points)

I made a module and added methods in it so it shortens up code to make it not so long.


So you have sort of a "common events" type module, metaphorically?

Neat! That's a good idea ^_^
... (<<<<<<<<<<<<<<< TEH DOTS OF DOOM. Hey, kinda catchy. :naughty:)

G_G

In case you didnt see the rest of the above post....

And I do it for other things. Use this
module A
 def self.ls(id, iid)
   $game_actors[id].learn_skill(iid)
 end
end


Then use A.ls(actor_id, skill_id)
what this does is instead of using
$game_actors[actor id].learn_skill(skill id)
you can use this instead
A.ls(actor id, skill id)

Seox

Quote from: game_guy on June 17, 2009, 09:44:05 pm
In case you didnt see the rest of the above post....

And I do it for other things. Use this
module A
 def self.ls(id, iid)
   $game_actors[id].learn_skill(iid)
 end
end


Then use A.ls(actor_id, skill_id)
what this does is instead of using
$game_actors[actor id].learn_skill(skill id)
you can use this instead
A.ls(actor id, skill id)



O_o

That's nifty. Thanks! ^_^



I'm trying to think of something that may be useful to people....
... (<<<<<<<<<<<<<<< TEH DOTS OF DOOM. Hey, kinda catchy. :naughty:)

Blizzard

June 18, 2009, 04:23:29 am #5 Last Edit: June 18, 2009, 04:24:31 am by Blizzard
.each .any? .all? .find_all <3

Definitely saving results of time consuming actions and proper class hierarchy to localize functionality and responsibilities of the different classes.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

fugibo

Quote from: Blizzard on June 18, 2009, 04:23:29 am
.each .any? .all? .find_all <3

Definitely saving results of time consuming actions and proper class hierarchy to localize functionality and responsibilities of the different classes.


HOLY CRAP, I <3 Array#find_all!!!!! I HAD NO IDEA THAT EXISTED!!! *levels blizz up*

My favorite feature is the dynamic typing. Horribly, horribly slow, but amazing useful, especially in RGSS.

Also: GG, BAD BAD BAD!!!! You know that writing methods to take the place of single lines of code is wrong! Stopitrightthisinstant!

Blizzard

Depends on how far you want to go with object oriented design. But in RGSS you shouldn't use it unless it simplifies your code a lot. And that piece of code is obviously pointless. xD *slaps G_G*
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Ryex

wait .find_all what dose that do? and ya G_G the code is pointless, you increase processing time and add four lines of code to shorten one line of code by six characters
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

fugibo

Quote from: Ryexander on June 18, 2009, 02:26:52 pm
wait .find_all what dose that do? and ya G_G the code is pointless, you increase processing time and add four lines of code to shorten one line of code by six characters


Here's an example that should clarify:

array = [1, 2, 3, 4, 5]
array2 = array.find_all { |n| n > 2 }
array3 = array.find_all { |n| n < 4 }

print array; print array2; print array3;

Seox

June 18, 2009, 03:08:00 pm #10 Last Edit: June 18, 2009, 03:11:57 pm by Seox
Still, I think the underlying point is noteworthy - that you can create a module with common and long methods, and then use them.

Dynamic typing?

Quote from: Blizzard on June 18, 2009, 04:23:29 am
.each .any? .all? .find_all <3

Definitely saving results of time consuming actions and proper class hierarchy to localize functionality and responsibilities of the different classes.


Aren't those of module Enumerable? Does that have to be mixed in/can it be mixed in to RGSS stuffs? I just looked em up, and they ALL look useful, especially .any? and .all?
... (<<<<<<<<<<<<<<< TEH DOTS OF DOOM. Hey, kinda catchy. :naughty:)

Ryex

Quote from: Longfellow on June 18, 2009, 03:05:03 pm
Quote from: Ryexander on June 18, 2009, 02:26:52 pm
wait .find_all what dose that do? and ya G_G the code is pointless, you increase processing time and add four lines of code to shorten one line of code by six characters


Here's an example that should clarify:

array = [1, 2, 3, 4, 5]
array2 = array.find_all { |n| n > 2 }
array3 = array.find_all { |n| n < 4 }

print array; print array2; print array3;



WHOOO! so it finds n where n > 2 and returns an array with all entries? COOL! THAT'S SO USEFUL!

so could you do this?

array = ['hi', 'book', 'name', 'book', 'hello', 'book']
array2 = array.find_all { |n| n == 'book' }
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

Seox

June 18, 2009, 04:28:11 pm #12 Last Edit: June 18, 2009, 04:30:38 pm by Seox
Quote from: Ryexander on June 18, 2009, 04:22:12 pm
Quote from: Longfellow on June 18, 2009, 03:05:03 pm
Quote from: Ryexander on June 18, 2009, 02:26:52 pm
wait .find_all what dose that do? and ya G_G the code is pointless, you increase processing time and add four lines of code to shorten one line of code by six characters


Here's an example that should clarify:

array = [1, 2, 3, 4, 5]
array2 = array.find_all { |n| n > 2 }
array3 = array.find_all { |n| n < 4 }

print array; print array2; print array3;



WHOOO! so it finds n where n > 2 and returns an array with all entries? COOL! THAT'S SO USEFUL!

so could you do this?

array = ['hi', 'book', 'name', 'book', 'hello', 'book']
array2 = array.find_all { |n| n == 'book' }



I would think so. Hold up, I'll try it in IRB.

Yeah, it works, returning:
# =>  array2 = ['book', 'book', 'book']
... (<<<<<<<<<<<<<<< TEH DOTS OF DOOM. Hey, kinda catchy. :naughty:)

fugibo

June 18, 2009, 04:48:02 pm #13 Last Edit: June 18, 2009, 04:53:05 pm by Longfellow
Another feature you guys might find useful is the RegExp comparison operator (=~), which checks a string against a Regular Expression (Zeriab posted a tutorial on that somewhere around here).


STRINGS = [ "Hello, Calintz! What's up? Haven't seen you in IRC in a while, lol. Hey, have you read the Necronomicon? BEST BOOK EVAR!", "You know something? My car was just stolen.", "HEY OLD MAN, DOWN IN FRONT!", "Man, that chick is HAWT." ]

book_references = STRINGS.find_all? {|s| s =~ /book/i} # The "i" at the end means case-insenstive.
man_references = STRINGS.find_all? {|s| s =~ /man/i}

print book_references; print man_references;


Also, *levels up for installing full ruby*

EDIT:
Oops, missed the "Dynamic Typing" thing. Googling it should yield decent results, but in summary it's the nature of a language that allows everything to change at any given moment, for example how you can redefine methods and classes. It especially refers to languages where this can be done at run-time.

EDIT2: Ah, no-one will ever know of his poor car :(

Seox

XD


Thank you for the level ^_^

I can see =~ being useful in ruby proper, but not nearly as much in RMXP. What do you use it for, specifically?
... (<<<<<<<<<<<<<<< TEH DOTS OF DOOM. Hey, kinda catchy. :naughty:)

fugibo

Quote from: Seox on June 18, 2009, 06:20:05 pm
XD


Thank you for the level ^_^

I can see =~ being useful in ruby proper, but not nearly as much in RMXP. What do you use it for, specifically?


Once I used to cheat on a really crappy assignment in Health class. You see, we were supposed to complete a daily journal of our health habbits (or something) for one month. Except, I didn't do it. I also didn't feel like typing up 31 pages of crap I did. So, I made one text file that looked kinda like this:

[month] [day], 2005

6:00: Woke up, [verb1], [verb2] [noun1] for breakfast.
<etc etc for the entire day>


Then came up with some random verbs/nouns and made a Ruby script that detected []'s and filled them in from a hash. Took a tenth of the time doing it manually would've taken, and it was more fun XD

Seox

Quote from: Longfellow on June 18, 2009, 07:17:29 pm
Quote from: Seox on June 18, 2009, 06:20:05 pm
XD


Thank you for the level ^_^

I can see =~ being useful in ruby proper, but not nearly as much in RMXP. What do you use it for, specifically?


Once I used to cheat on a really crappy assignment in Health class. You see, we were supposed to complete a daily journal of our health habbits (or something) for one month. Except, I didn't do it. I also didn't feel like typing up 31 pages of crap I did. So, I made one text file that looked kinda like this:

[month] [day], 2005

6:00: Woke up, [verb1], [verb2] [noun1] for breakfast.
<etc etc for the entire day>


Then came up with some random verbs/nouns and made a Ruby script that detected []'s and filled them in from a hash. Took a tenth of the time doing it manually would've taken, and it was more fun XD


Oh, that's EPIC. I totally have to try that, XD. I wrote a ruby program a while back that acts as flash cards, which I'll use when school starts again. probably never. I can't/don't study. Regardless, pretty cool stuff. It stores missed problems in an array, and then tells you which you missed at the end, too.
... (<<<<<<<<<<<<<<< TEH DOTS OF DOOM. Hey, kinda catchy. :naughty:)

Blizzard

June 19, 2009, 04:52:18 am #17 Last Edit: June 19, 2009, 04:55:10 am by Blizzard
I use regular expressions in RMX-OS to check for messages from the clients.

	case message
when /REG(.+),(.+)/ # register request
result = self.try_register($1, $2)
# if registering was successful
if result == RESULT_SUCCESS
# log in as well
self.try_login($1, $2)
# send username and user ID
self.send("USR#{@username}")
self.send("UID#{@user_id}")
end
self.send("REG#{result}")
return true
end


And yes, .any?, .all?, etc. are from Enumerable. For them to work, .each needs to be defined in a class and Enumerable needs to be included I think.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Seox

... (<<<<<<<<<<<<<<< TEH DOTS OF DOOM. Hey, kinda catchy. :naughty:)

Zeriab