Hi thanks for taking a look at my question. I'm trying to create a site with a Sinatra server that will allow users to run ruby code in the browser similar to what you see with repl.it, code academy, etc.
Is there a gem/library or anything out there can can handle the interpreter? If I need to create one myself, how can I do this without evaluating the code in the server? (seems dangerous)
Any advice is helpful, thanks!
You can use repl.it's API https://repl.it/api
Should be very easy to use
You can use Kernel#eval to run code from the string (user's input). Remember that it is not safe to execute some random code directly in your production environment.
Related
I'm writing an application in ruby and would like to access some legacy code written in another language (php). Unfortunately this legacy code does not have an HTTP API, but it is living on the same file system. I had the idea that maybe instead of building an API, or rewriting all of the code in ruby, there may be some clever way I can expose these PHP functions so ruby can access them. Do you have an idea or approach I can use to accomplish this?
Assuming you mean code that is callable at the command-line, using either backticks or the %x operator or the more complex system, popen and popen3 commands lets you execute separate pieces of code and gather the results.
For instance in IRB:
irb(main):002:0> puts `ls`
Desktop
Documents
Wrap that in a method and it becomes a way to call an external program:
def ls(s='')
`ls #{s}`
end
ls() # => "Desktop\nDocuments\nDownloads\nLibrary\nMovies\nMusic\nPictures\nPublic\nSites\nVirtualBox VMs\nbin\ndevelopment\nlibsmi\nperl5\nproduction\nshare\ntest.data\ntmp\n"
ls('M*') # => "Movies:\n\nMusic:\niTunes\n"
See "Ruby, Difference between exec, system and %x() or Backticks" for additional information.
The only reasonable solution I can think of is to rewrite chunks of the php or to write an http api.
If you want to embark upon a more adventurous route you could take a look compiling the php into exes (Convert a PHP script into a stand-alone windows executable) and then execute them via backticks, system, or etc...
It probably would be best to rewrite it though.
I am writing an open source gem that interacts with an sms service. I want to test the interaction, however it needs account information and a phone number to run. It also needs feedback to determine if sms messages were being sent correctly. This causes two problems:
I can't put the account information in the test file, as the gem is open source and anyone could get to it.
I need the person running the test to give information to the script as it is running (eg checking the phone to see if a message was received).
What techniques or libraries are available that can help with this? I'm currently using rspec and making it prompt for parameters (using gets), however it is pretty cluncky at the moment. I can't be the first person using ruby to have this problem, and I feel that I'm missing a gem or something that solves this problem.
Use mocks
What are your tests testing, specifically? That a given login/password works? Probably not. Most likely you want to make sure your code reacts to the API properly. Therefore, I'd suggest mocking. Save the output of the API calls and use a mock service to return those responses. Then test. Your tests will be faster and less brittle as a happy side-effect.
More information on mocking with RSpec is here:
http://rspec.info/documentation/mocks/
Re 1) Why not just save configuration options in a YAML file and load them at the beginning of your tests?
Re 2) Are there maybe any web services for that? E.g. one where you can send a message to and query an API to see if it worked. I know this can be unreliable, but the same is true for a user's phone company network.
+1 for Mark Thomas' answer on mocking. Two more alternative mock object libraries for Ruby: FlexMock and Mocha
I have a small Ruby script that I want to run by visiting a page in a browser. This might seem like a dumb question but what's the easiest way to accomplish this? I haven't found very good documentation on how to get Ruby working with Apache so I'm open to any suggestions at this point.
I suggest Sinatra. As shown on that page, it's very lightweight, and Apache is not even necessary. As you get to needing more performance you might use Nginx or Apache as a reverse proxy (serving your static files quickly) and something like Thin to make your application run faster.
But for now, just start using Sinatra. As shown on the home page, you can get started in just a few lines.
After being blown away by the greatness of irb and rails console, I am loving the dynamical parts of the language more and more. Now, I wonder if it would be possible for a Ruby application to update itself on-the-fly (if the write permission allows it). Can a Ruby application fetch an update and refresh itself? I have seen this functionality a lot in Apple applications and I know updates and fixes are something you must anticipate for when deploying an application.
Thank you for your feedback, comments and answers!
Sure. You can load a file that re-opens an existing class and alters behavior. There are a few ways to get the new code, too. Read it off disk, or have a socket that accepts connections and then eval the strings passed in. Or use HTTP: http://www.neurogami.com/articles/The_year_of_living_dangerously/
BTW, there's some risk involved with doing thngs this way. :)
What is a good approach to a client proxy written in ruby that I can use to create a custom filter.
So far I've found
Ruby Proxy using webrick
Mousehole, a scriptable Ruby proxy by _why (UPDATE this was not robust)
A little on the fringe, this guy wants to Use rack as thin proxy with his question. I don't think he got an answer; or even a hint that it was possible.
What is your advice on these suggested approaches or do you have a better approach.
Thanks!
I can’t speak on personal experience as I’ve not done this myself, but I have heard of mouseHole before and it seems to be a good package. Why not try writing a simple script for it and see how you find it?
There are also some sample scripts in that repository that you could check out.