[#362083] Teaching Programming Languages (including Ruby) — Samuel Williams <space.ship.traveller@...>

Hello,

20 messages 2010/05/02

[#362098] Main working window for Ruby is DOS? — Kaye Ng <sbstn26@...>

I know nothing about programming and am not a techy person, so please

16 messages 2010/05/03

[#362116] School teacher still at it learning programming language — Hilary Bailey <my77elephants@...>

Now I while glimpsing at the beauty of Ruby, there is the software of

11 messages 2010/05/03

[#362166] Something I expected to work, but didn't! — Kurtis Rainbolt-greene <kurtisrainboltgreene@...>

irb(main):001:0> x = 2

11 messages 2010/05/04

[#362215] for-in vs. map closures — Mike Austin <mike_ekim@...>

I was experimenting with closures and JavaScript's and Ruby's

11 messages 2010/05/05

[#362286] ri on sqlite — Intransition <transfire@...>

What do others think of a creating a new ri tool which uses a SQLite

17 messages 2010/05/06

[#362341] ease of porting (translating) ruby to C (vs. python)? — bwv549 <jtprince@...>

In a very small bioinformatics group I know of, they are deciding

17 messages 2010/05/07

[#362375] Strings iteration — Viorel <viorelvladu@...>

I have some names like aaxxbbyy where xx is '01'..'10' and yy is also

14 messages 2010/05/08

[#362425] Any future for curses applications/toolkits like rbcurse ? — "R. Kumar" <sentinel.2001@...>

Have apps moved over to the web (or GUI) totally ? Will there be any

21 messages 2010/05/10
[#362441] Re: Any future for curses applications/toolkits like rbcurse ? — botp <botpena@...> 2010/05/10

On Mon, May 10, 2010 at 2:13 PM, R. Kumar <sentinel.2001@gmx.com> wrote:

[#362448] Re: Any future for curses applications/toolkits like rbcurse ? — "R. Kumar" <sentinel.2001@...> 2010/05/10

interface and/or the installation itself is terrible.

[#362458] Re: Any future for curses applications/toolkits like rbcurse ? — botp <botpena@...> 2010/05/10

On Mon, May 10, 2010 at 10:28 PM, R. Kumar <sentinel.2001@gmx.com> wrote:

[#362460] Re: Any future for curses applications/toolkits like rbcurse ? — "R. Kumar" <sentinel.2001@...> 2010/05/10

botp wrote:

[#362463] Re: Any future for curses applications/toolkits like rbcurse ? — "R. Kumar" <sentinel.2001@...> 2010/05/10

Strange. I cant push a gem even after yanking.

[#362452] Unit Test of method calling system() - how? — Martin Hansen <mail@...>

How can I unit test the two methods:

16 messages 2010/05/10

[#362498] In Ruby, can the coerce() method know what operator it is th — Jian Lin <blueskybreeze@...>

In Ruby, it seems that a lot of coerce() help can be done by

12 messages 2010/05/11
[#362546] Re: In Ruby, can the coerce() method know what operator it is th — Caleb Clausen <vikkous@...> 2010/05/11

On 5/10/10, Jian Lin <blueskybreeze@gmail.com> wrote:

[#362611] Re: In Ruby, can the coerce() method know what operator it is th — Colin Bartlett <colinb2r@...> 2010/05/12

On Tue, May 11, 2010 at 4:46 PM, Caleb Clausen <vikkous@gmail.com> wrote:

[#362657] Asynchronous HTTP request — Daniel DeLorme <dan-ml@...42.com>

Does anyone know how to do the following, but without threads, purely

28 messages 2010/05/13

[#362718] Range on strings. — Vikrant Chaudhary <nasa42@...>

Hi,

13 messages 2010/05/14

[#362787] class best way for getters ? — unbewusst.sein@... (Une B騅ue)

i have a class "HFSFile" initialized by a parsed string

12 messages 2010/05/15

[#362979] curl library? — Xeno Campanoli / Eskimo North and Gmail <xeno.campanoli@...>

Two questions:

14 messages 2010/05/18
[#362980] Re: curl library? — Xeno Campanoli / Eskimo North and Gmail <xeno.campanoli@...> 2010/05/18

On 10-05-18 02:35 PM, Xeno Campanoli / Eskimo North and Gmail wrote:

[#362982] Re: curl library? — Luis Parravicini <lparravi@...> 2010/05/18

On Tue, May 18, 2010 at 6:56 PM, Xeno Campanoli / Eskimo North and

[#362984] Re: curl library? — Xeno Campanoli / Eskimo North and Gmail <xeno.campanoli@...> 2010/05/18

Well, I got that -dev thing installed with apt-get, and then I tried again and

[#363027] Retrieve instance — Walle Wallen <walle.sthlm@...>

Quick question. Can I somehow retrieve the instance of the class Test in

11 messages 2010/05/19

[#363076] Scrape javascript content — Phil Mcdonnell <phil.a.mcdonnell@...>

I'm trying to scrape a page that hides some data behind a javascript

11 messages 2010/05/20

[#363115] OMG, why are there so many Strings in ObjectSpace! — timr <timrandg@...>

I was playing around looking at ObjectSpace in irb and was astounded

14 messages 2010/05/21

[#363225] Redefine a Class? — Mark T <paradisaeidae@...>

Currently this raises: superclass mismatch for class Soda (TypeError)

12 messages 2010/05/25

[#363240] Funny IO.select behaviour — Dennis Nedry <dennis@...>

I've been debugging my full screen console ruby editor.

13 messages 2010/05/25

[#363348] Ruby as Client Side Language in Web Browser (replacing JS) — "Simone R." <k5mmx@...>

Hi everybody,

17 messages 2010/05/27

[#363412] A better way to write this function? — Jason Lillywhite <jason.lillywhite@...>

Here is my attempt at Newton's second law in Ruby:

14 messages 2010/05/28

[#363417] Interrupting the evaluation of a ruby script — Emmanuel Emmanuel <emmanuel.bacry@...>

This is my problem :

12 messages 2010/05/28
[#363447] Re: Interrupting the evaluation of a ruby script — Branden Tanga <branden.tanga@...> 2010/05/28

Emmanuel Emmanuel wrote:

[#363483] Re: Interrupting the evaluation of a ruby script — Emmanuel Emmanuel <emmanuel.bacry@...> 2010/05/29

[#363426] A complete beginners question — Ant Walliams <anthonywainwright@...>

Hi there,

19 messages 2010/05/28

[#363432] Dynamic SVG with Ruby/Tk — Yotta Meter <spam@...>

The example I'm looking for in regards to ruby/SVG differs from the

14 messages 2010/05/28

[#363467] Date.today problem on linux with Ruby 1.8.6 — Jarmo Pertman <jarmo.p@...>

Hello.

10 messages 2010/05/29

[#363524] enumerator problem in 1.9.1 — Bug Free <amberarrow@...>

The following line:

19 messages 2010/05/31
[#363528] Re: enumerator problem in 1.9.1 — botp <botpena@...> 2010/05/31

On Mon, May 31, 2010 at 9:04 AM, Bug Free <amberarrow@yahoo.com> wrote:

[#363533] Re: enumerator problem in 1.9.1 — Robert Klemme <shortcutter@...> 2010/05/31

2010/5/31 botp <botpena@gmail.com>:

[QUIZ][SUMMARY] Solar System (#227)

From: Daniel Moore <yahivin@...>
Date: 2010-05-10 20:30:33 UTC
List: ruby-talk #362484
There were two solutions for this quiz, a mathematical computation
approach, and a web services based approach.

Glen F. Pankow used a mathematical approach from the NASA Horizons
site as described in Formulae for Computing Approximate Planetary
Positions (http://ssd.jpl.nasa.gov/txt/aprx_pos_planets.pdf). Glen=92s
program makes use of a table of data containing the Keplerian elements
(http://en.wikipedia.org/wiki/Orbital_elements#Keplerian_elements) of
each planetary body. Then when given the date in question as input,
the numbers are crunched to produce the x, y, and z positions of each
planetary body at that point in time. The final step is to compute the
distance from the position of the Earth to each of those positions and
display the results.

The mathematical formulas involved in computing the positions can be
daunting, so a tip of the hat to Glen for writing them up with good
comments in a step by step solution. Glen=92s solution is certainly
worth taking a look at if you are interested in the mathematics of
planetary motion.

Jean Lazarou provided a web-services based solution that queried the
NASA Horizons system directly as well as a summary of the solution.

*The following section of the summary is written by Jean Lazarou.*

After asking a friend, who pointed me to the NASA horizons system site
which provides solar system data, I realised that a simple solution
would be to use the web version of the NASA=92s site
(http://ssd.jpl.nasa.gov/?horizons) tool to retrieve the distances.


Understanding a site=85

After playing a little bit with horizons I was able to use it to
retrieve interactively the distance from earth to any planet of our
solar system at a given date.

The user sets an amount of parameters first that define some query and
then he/she asks for the result.

Horizons seems to store in a HTTP session the parameters that define
what data we want to retrieve. It has several group of parameters like
the result format, the target planet, the origin planet, etc. Each
group may contain several parameters. Because we can query only one
planet at a time, we must submit eight queries to retrieve all the
distances.

To implement the solution we must find how our program can communicate
with the server. Using a Firefox addon, HttpFox for instance, we can
analyse the underlyingHTTP data. We record all the necessary postings,
then we locate the values we need to change (the date and the
planets). To retrieve the results we need, our program parse the
response returned by the server. As Horizons can return simple text,
parsing the result is easier.

The horizons web application expects post requests using form-data as
multipart format. Because the standard HTTP layer in Ruby does not
support multipart data transfer, we are going to add a simple support
for it.


Multipart form-data

Before presenting how we can add multipart support, let us first see
one way of using the HTTP library (using Ruby 1.8).


Posting data with Ruby=92s HTTP library

Next snippet is a quick overview of a post request using the HTTP library.

01 require 'net/http'
02
03 server =3D 'mys_server.com'
04 port =3D '8080'
05
06 http =3D Net::HTTP.new(server, port)
07
08 path =3D '/submit'
09 content =3D 'name=3Dme'
10 headers =3D {''Content-Type' =3D> 'application/x-www-form-urlencoded'}
11
12 response, body =3D http.post(path, content, headers)
13
At line 6 we create an HTTP object. At line 12 we send a POST request
with one parameter.

We could write the same code using other APIs from the HTTP library,
we wrote it this way as a base for what follows.


Extending the default library

We are not going to extend the classes used by the HTTP library, but
specifically add the multipart support to the objects we create. We
define a kind of factory method named create_http_with_multipart.

01 require 'net/http'
02
03 def create_http_with_multipart server, port =3D nil
04
05   http =3D Net::HTTP.new(server, port)
06
07   def http.post_multipart path, parameters, headers =3D {}
08   end
09
10   http
11
12 end
Line 10 returns a normal HTTP object with one more method named
post_multipartadded to the new HTTP object at line 7.

The post_multipart method expects the server path for the request, a
hash object with the parameters and a hash object with the optional
header parameters. (It does not support files as parameters.)

01   def http.post_multipart path, parameters, headers =3D {}
02
03     boundary =3D 'xxboundaryxx'
04
05     data =3D []
06
07     parameters.each do |name, value|
08       data << "--#{boundary}"
09       data << "Content-Disposition: form-data; name=3D\"#{name}\""
10       data << ''
11       data << value.to_s
12     end
13
14     data << "--#{boundary}"
15     data << ''
16
17     body =3D data.join("\r\n")
18
19     headers['Content-Type'] =3D "multipart/form-data; boundary=3D#{bound=
ary}"
20     headers['Content-Length'] =3D body.length.to_s
21
22     return post(path, body, headers)
23
24   end
The implementation loops through all the parameters (line 7) filling
an array (data) and adding each time a new part (starting with the
boundary sequence =96 line 8). It adds a last boundary after the loop,
before converting the array to a string (lines ended with carriage
return / line feed =96 line 17).

Then it adds the necessary headers, notice the one setting the content
type with the boundary string (line 19).

And finally it calls the inherited post method (line 22).


Back to distance=85

We hide the complexity of the horizons site communication in the
Horizons class. It initializes the parameters in the server=92s session,
stores the necessary cookie, makes the eight requests and parses the
result. Parsing the result is very simple as the horizons encloses the
data table between two lines containing $$SOE and $$EOE.

The main script uses the Horizons class and loops waiting for user to
input dates.


Conclusion

The result is rather slow (mainly because we need to make eight requests).

We didn=92t really solve the problem. But we didn=92t reinvent the wheel,
was it possible? ;)

Here is a run result, so that you can check the planet positions on
the 2nd of May (distances may seem short but they are expressed in AU
=96 astronomical unit).

$ ruby solar.rb
Enter a date or nothing to quit
Date (YYYY-MM-DD): 2010-05-02
  JUPITER: 5.60264666760831 AU
  VENUS: 1.45723642739716 AU
  SATURN: 8.75025556531919 AU
  MERCURY: 0.56158052050790 AU
  URANUS: 20.8265012641112 AU
  PLUTO: 31.2222615921462 AU
  MARS: 1.29568386653263 AU
  NEPTUNE: 30.2996308594089 AU

  (1 AU =3D 149,597,870.691 km)
Date (YYYY-MM-DD):
$

Solar System (#227) - Solutions (http://rubyquiz.strd6.com/quizzes/227.tar.=
gz)

In This Thread

Prev Next