Gammon Forum
Entire forum
MUSHclient
Suggestions
Ruby revisted
It is now over 60 days since the last post. This thread is closed.
  Refresh page
Pages: 1
2 3
Posted by
| Ariesroyaal
(20 posts) bio
|
Date
| Reply #15 on Wed 09 May 2007 12:18 PM (UTC) |
Message
| Nick, forget the GRuby implementation. I figured it out. Should have thought of it before really. try
def self.sayhello
@world.note("hello world!")
end | top |
|
Posted by
| Nick Gammon
Australia (22,973 posts) bio
Forum Administrator |
Date
| Reply #16 on Wed 09 May 2007 04:30 PM (UTC) |
Message
| OK, so that means that the version you have works then? Since it uses "RubyScript" already?
Maybe if you did a few posts showing how to do some general things in Ruby (like some of the posts about Lua for example), that might help others get started. :) |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | top |
|
Posted by
| Shaun Biggs
USA (644 posts) bio
|
Date
| Reply #17 on Thu 10 May 2007 06:47 AM (UTC) |
Message
| I'd love to see if I can get ruby working on MUSHclient through Wine. Granted, this means I'll have to start working on learning ruby again, but it was a fun language while I was using it. Does this mean we will be getting a ruby board here once enough people have posted questions and feedback notes?
Ariesroyaal: Please let us know how you got ruby working. I'm going to have to jump through a few more hoops than you to set it up most likely, but I think it might be worth the extra effort. |
It is much easier to fight for one's ideals than to live up to them. | top |
|
Posted by
| Ariesroyaal
(20 posts) bio
|
Date
| Reply #18 on Thu 10 May 2007 12:43 PM (UTC) |
Message
| I -have- run into a few hangups. Particularly you cannot call methods from within the script. Still trying to figure that one out. Nick, do you have a client with the GRScript on it I can mess with as well? maybe I can get that one to work more predictably. | top |
|
Posted by
| Ariesroyaal
(20 posts) bio
|
Date
| Reply #19 on Thu 10 May 2007 12:46 PM (UTC) |
Message
| nevermind, I was probably smoking something. It works. Heres a simple example.
def self.crunchy_bacon
string = "CrUnChy BaCOn"
@world.note(string)
@world.note(string.reverse)
@world.note(string.swapcase)
end
self.crunchy_bacon | top |
|
Posted by
| Ariesroyaal
(20 posts) bio
|
Date
| Reply #20 on Thu 10 May 2007 12:49 PM (UTC) |
Message
| a note:
ActiveScript Ruby doesnt seem to run scripts in the same way ruby does. In a normal Ruby environment you can call a method anywhere in a script as long as it is defined someplace in the script. In MUSHClient you will need to define any methods you are going to call from within the script before you call them. | top |
|
Posted by
| Shaun Biggs
USA (644 posts) bio
|
Date
| Reply #21 on Thu 10 May 2007 06:43 PM (UTC) |
Message
| That's kind of odd. Is this similar to those old procedural languages with having to define everything top down? It's probably just how ActiveRuby loads the script files. Might want to check around to see if it's just a MUSHclient thing, or if it's a common occurrence.
Also, I notice that you keep using "def self.foo" for functions. Is this necessary, or can you just do "def foo" for local functions? |
It is much easier to fight for one's ideals than to live up to them. | top |
|
Posted by
| Ariesroyaal
(20 posts) bio
|
Date
| Reply #22 on Thu 10 May 2007 07:23 PM (UTC) |
Message
| Because of the ActiveRuby engine you have to define self on all functions. This assigns the method as an instance method to the parent class. Its a slight pain, but worth it to me to be able use Ruby as a scripting language. | top |
|
Posted by
| Ariesroyaal
(20 posts) bio
|
Date
| Reply #23 on Thu 10 May 2007 09:11 PM (UTC) |
Message
| Upon further investigation the reason self needs to be called is primarily because
def crunchy_bacon
end
this is an instance method, and is only callable in an actual instance of a parent module (Module). The script is more or less an uninstantiated class so everything needs to be defined as a class method.
There are still hangups... any chance of getting the GRScript version still? | top |
|
Posted by
| Nick Gammon
Australia (22,973 posts) bio
Forum Administrator |
Date
| Reply #24 on Fri 11 May 2007 02:29 AM (UTC) |
Message
| As I said before, if I use the GlobalRubyScript version, I find I can't connect to the MUD.
I would have thought that the script engine implementation should have worked more smoothly than that. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | top |
|
Posted by
| Ariesroyaal
(20 posts) bio
|
Date
| Reply #25 on Fri 11 May 2007 01:04 PM (UTC) |
Message
| what does it throw when you try and connect? | top |
|
Posted by
| Ariesroyaal
(20 posts) bio
|
Date
| Reply #26 on Fri 11 May 2007 01:33 PM (UTC) |
Message
| also, what how are you defining world when instantiating ruby? | top |
|
Posted by
| Ariesroyaal
(20 posts) bio
|
Date
| Reply #27 on Fri 11 May 2007 04:26 PM (UTC) Amended on Fri 11 May 2007 10:06 PM (UTC) by Nick Gammon
|
Message
| Ruby Finale
Alright so heres what I came up with for ruby scripting. There are various little humps to get over to get a ruby script to behave with MUSHClient, and it is not for the faint of heart. While for my particular scripts I find useful to use Ruby despite the difficulties, I don't suggest using it unless you are wanting to work at it.
heres how it works out. first, MUSHClient evaluates a Ruby script in an uninstantiated class. This means that it does not see instance methods such as:
def hello_world
@world.note("hello world")
end
for this reason methods that you want your triggers, aliases and whatever else to call from MUSHClient require you to define them like this:
def self.hello_world
@world.note("hello world")
end
not so hard yet right? heres the tricky part. because it is an uninstantiated class it is executed in a strictly linear fashion. so this wouldn't work.
self.hello_world
def self.hello_world
@world.note("hello world")
end
for this reason, you need a second subclass for special 'global' methods. Lets call this class Global:
class Global
def hello_world
return "hello world"
end
end
Now make a new instance of the global class:
@global = Global.new
#make sure you use the @, you want it to be accessible to everything from here on out. you can use $ as well.
now lets make some calls
def self.hello_world
@world.note(@global.hello_world)
end
so lets see a real world example:
#create a global version of @world so that my
#core class can use it.
$world = @world
#heres our core class, this is where the real work is done
class Core
#we need to track balance, so lets set up some balance
#attributes complete with callbacks when they are
#changed! (this is why I LOVE Ruby)
attr_accessor :balance, :potion_balance,
:herb_balance, :all _balance
#each of the following are called when the particular
#attribute is assigned
def balance=(state)
@balance = state
evaluate_balance
end
def herb_balance=(state)
@balance = state
evaluate_balance
end
def potion_balance=(state)
@balance = state
evaluate_balance
end
#dynamically decide the value of full_balance
def full_balance
if (potion_balance and balance and
herb_balance)
@full_balance = true
else
@full_balance = false
end
end
#this is called everytime a balance is set
def evaluate_balance
#do cool balancing stuff here
end
end
#now we're back in the main class where MUSHClient calls are
#defined
@core = Core.new
def self.set_balance(name, line, wilds)
#for simplicities sake lets say that the wilds
#are the actual states the balance is at.
@core.balance = wilds[0]
@core.herb_balance = wilds[1]
@core.potion_balance = wilds[3]
$world.note(full_balance.to_s) #==> true
end
| top |
|
Posted by
| Nick Gammon
Australia (22,973 posts) bio
Forum Administrator |
Date
| Reply #28 on Fri 11 May 2007 10:11 PM (UTC) |
Message
| Thanks for that - I tidied up the forum tags a bit.
Can you give the recommended format for a list of constants? See this page:
http://www.gammon.com.au/scripts/function.php?action=errors
If you can give an example of how that would look in Ruby (a couple of lines will do), I can make a Ruby version of the constants list. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
79,133 views.
This is page 2, subject is 3 pages long:
1
2 3
It is now over 60 days since the last post. This thread is closed.
  Refresh page
top
Quick links:
MUSHclient.
MUSHclient help.
Forum shortcuts.
Posting templates.
Lua modules.
Lua documentation.
Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.