I'm trying to make a rouguelike game that runs inside a terminal using Ruby but I'm not exactly sure how to go about doing that. I want to be able to address and update each cell in the standard 80*24 terminal window individually. Can I do this with the standard library or alternately are there any good gems I could do this with?
Curses is probably the easiest to implement and it is widely available across platforms. Ruby bindings used to come as part of the standard library, but it's now a gem: gem install curses. Here's an example from the docs:
require "curses"
def show_message(message)
height = 5
width = message.length + 6
top = (Curses.lines - height) / 2
left = (Curses.cols - width) / 2
win = Curses::Window.new(height, width, top, left)
win.box("|", "-")
win.setpos(2, 3)
win.addstr(message)
win.refresh
win.getch
win.close
end
Curses.init_screen
begin
Curses.crmode
Curses.setpos((Curses.lines - 1) / 2, (Curses.cols - 11) / 2)
Curses.addstr("Hit any key")
Curses.refresh
Curses.getch
show_message("Hello, World!")
ensure
Curses.close_screen
end
You can use Gosu.
You can find more alternatives in ruby toolbox - game libraries.
I'm assuming you're using a linux. For manipulating the terminal you will need the ncurses library bindings for Ruby. See ncurses-ruby.
The documentation is sparse, but there are plenty of examples at this github repository.
Related
I am using GDAL 1.7.1 from ruby1.9 to generate GeoTIFF files. In the tutorial they recommend to use GDALClose() to close the datasets and flush any remaining content to the filesystem. The same happens in the destructor for the dataset. The problem is that the ruby bindings rely on this destructor mechanism to close the dataset, and I need the result of the file already in the process that generates it. Since ruby is garbage collected, it seems I can not reliably close my files, without exiting the ruby process. For now I patched my version of GDAL to support the GDALClose method, but this doesn't seem to be a good long term solution.
require 'gdal/gdal'
[...]
# open the driver for geotiff format
driver = Gdal::Gdal.get_driver_by_name('GTiff')
# create a new file
target_map = driver.create(output_path,
xsize,
ysize, 3,
Gdal::Gdalconst::GDT_UINT16, ["PHOTOMETRIC=RGB"])
# write band data
3.times do |i|
band = target_map.band(i + 1)
target_map.write_band(i + 1, mapped_data)
end
# now I would like to use the file in output_path, but at this point
# large parts of the data still resides in memory it seems until
# target_map is destroyed
file = File.open( output_path, "r" )
[...]
Is there something in either ruby or swig to force the destructor call, that I may have overlooked?
Normally what is done with the GDAL bindings in Python is to set the objects to None. So in Ruby, this would be nil:
band = nil
target_map = nil
It's a funny way to save/flush/close the data, but it is how it is done.
I want to visualize some of my statistical caluclations in Ruby.
My problem is, that I can't find the right gem for that.
rsruby doesn't seem to be up-to-date and I can't install it in Ruby 1.9.2.
Do you know a way how to run the R commands in Ruby?
I just saw this post and thought I should comment since I use R pretty extensively. If you are coming from an R background the best gem I have found is Rinruby. The reason it is fantastic is because you don't interpret the commands in ruby, you use actual R code. For example:
require "rinruby"
#Set all your variables in Ruby
n = 10
beta_0 = 1
beta_1 = 0.25
alpha = 0.05
seed = 23423
R.x = (1..n).entries
#Use actual R code to perform the analysis
R.eval <<EOF
set.seed(#{seed})
y <- #{beta_0} + #{beta_1}*x + rnorm(#{n})
fit <- lm( y ~ x )
est <- round(coef(fit),3)
pvalue <- summary(fit)$coefficients[2,4]
EOF
On the Rinruby website I listed above there are some fantastic examples to get you started. Hope this helped.
-Sean
As #Hansi mentioned, RServe is the best way I've found to run R remotely. If you're using Ruby from a web-context especially, RServe can offer some nice benefits.
Best of all (in my mind), you don't get locked into any one programming framework, as there are RServe clients for a variety of languages including Java and C++. When using web-accessible platforms, you can even keep Rserve running on a separate host and route traffic over TCP/IP for added security.
Is it possible to open every link in certain div and collect values of opened fields alltogether in one file or at least terminal output?
I am trying to get list of coordinates from all markers visible on google map.
all_links = b.div(:id, "kmlfolders").links
all_links.each do |link|
b.link.click
b.link(:text, "Norādījumi").click
puts b.text_field(:title, "Galapunkta_adrese").value
end
Are there easier or more effective ways how to automatically collect coordinates from all markers?
Unless there is other data (alt tags? elements invoked via onhover?) in the HTML already that you could pick through, that does seem like the most practical way to iterate through the links, however from what I can see you are not actually making use of the 'link' object inside your loop. You'd need something more like this I think
all_links = b.div(:id, "kmlfolders").links
all_links.each do |thelink|
b.link(:href => thelink.href).click
b.link(:text, "Norādījumi").click
puts b.text_field(:title, "Galapunkta_adrese").value
end
Probably using their API is a lot more effective means to get what you want however, it's why folks make API's after all, and if one is available, then using it is almost always best. Using a test tool as a screen-scraper to gather the info is liable to be a lot harder in the long run than learning how to make some api calls and get the data that way.
for web based api's and Ruby I find the REST-CLIENT gem works great, other folks like HTTP-Party
As I'm not already familiar with Google API, I find it hard for me to dig into API for one particular need. Therefor I made short watir-webdriver script for collecting coordinates of markers on protected google map. Resulting file is used in python script that creates speedcam files for navigation devices.
In this case it's speedcam map maintained and updated by Latvian police, but this script can probably be used with any google map just by replacing url.
# encoding: utf-8
require "rubygems"
require "watir-webdriver"
#b = Watir::Browser.new :ff
#--------------------------------
#b.goto "http://maps.google.com/maps?source=s_q&f=q&hl=lv&geocode=&q=htt%2F%2Fmaps.google.com%2Fmaps%2Fms%3Fmsid%3D207561992958290099079.0004b731f1c645294488e%26msa%3D0%26output%3Dkml&aq=&sll=56.799934,24.5753&sspn=3.85093,8.64624&ie=UTF8&ll=56.799934,24.5753&spn=3.610137,9.887695&z=7&vpsrc=0&oi=map_misc&ct=api_logo"
#b.div(:id, "kmlfolders").wait_until_present
all_markers = #b.div(:id, "kmlfolders").divs(:class, "fdrlt")
#prev_coordinates = 1
puts "#{all_markers.length} speedcam markers detected"
File.open("list_of_coordinates.txt","w") do |outfile|
all_markers.each do |marker|
sleep 1
marker.click
sleep 1
description = #b.div(:id => "iw_kml").text
#b.span(:class, "actbar-text").click
sleep 2
coordinates = #b.text_field(:name, "daddr").value
redo if coordinates == #prev_coordinates
puts coordinates
outfile.puts coordinates
#prev_coordinates = coordinates
end
end
puts "Coordinates saved in file!"
#b.close
Works both on Mac OSX 10.7 and Windows7.
I'm not a programmer, even amateur, I just wanted a program that would change PSP screen (whole) color as fast as possible infinitely. I made something:
rdupa = Image.load("red.png")
gdupa = Image.load("green.png")
bdupa = Image.load("blue.png")
screen:clear()
while true do
screen:blit(0, 0, rdupa, false)
screen:clear()
screen:blit(0, 0, gdupa, false)
screen:clear()
screen:blit(0, 0, bdupa, false)
screen:clear()
end
Using Google, but that doesn't work. What did I do wrong (I have *.png images in the same folder as script)? Ready script would be seen veeeeeeery nicely.
I'm not sure about your environment but I'd guess it's most likely unable to update its main window or whatever as it's essentially stuck executing the Lua code snipped (unless that's executed in a separate thread).
Lua is a small and concise programming language and provides only few core functions (see this list).
Lua does not provide any functions to work with screen and images by default, so without knowing what library/framework are you using, there is little we can do to help.
For this answer, I'm operating under the assumption that you're using Lua Player (consider adding a luaplayer tag?). From what I can see in the documentation, you should be using
screen.flip()
instead of
screen:clear()
whenever you want to update the screen. Unfortunately, I do not have CFW on my PSP, so I cannot test this myself.
To be honest, I wouldn't even consider using images.
something like this would do :)
(This is using PGELua but can easily be adapted for LuaPlayer)
while pge.running() do
color = pge.gfx.createcolor(pge.math.rand(255),pge.math.rand(255),pge.math.rand(255))
pge.gfx.startdrawing()
pge.gfx.drawrect(0,0,480,272,color)
pge.gfx.enddrawing()
pge.gfx.swapbuffers()
end
if you want defined colours, maybe something like.
red = pge.gfx.createcolor(255,0,0)
gre = pge.gfx.createcolor(0,255,0)
blu = pge.gfx.createcolor(0,0,255)
loop = 1
while pge.running() do
pge.gfx.startdrawing()
if loop==1 then
pge.gfx.drawrect(0,0,480,272,red)
elseif loop==2 then
pge.gfx.drawrect(0,0,480,272,gre)
elseif loop==3 then
pge.gfx.drawrect(0,0,480,272,blu)
end
loop=loop+1
if loop>4 then
loop=1
end
pge.gfx.enddrawing()
pge.gfx.swapbuffers()
end
Im looking for a lib that will warp my strings so they behave more like 1.9 so i can use them more easily in a 1.8/1.9 project e.g.
Lib.new("ääq").size == 3
James Gray wrote a series of good articles about dealing with Unicode and multi-byte characters that are good reading. In particular "The $KCODE Variable and jcode Library" and "Getting Code Ready for Ruby 1.9 " will help a lot.
Basically, you can add:
$KCODE = 'U'
to the top of your source file and inherit some new functionality for dealing with multibyte characters.
Just did it myself...
gem install string19
String19('áßð').size == 3
String19('áßð').index('ð') == 2
etc.
not all methods supported, but easy to add more