[#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>:

Re: Accessing elements of a ruby array with javascript

From: David Masover <ninja@...>
Date: 2010-05-16 03:31:15 UTC
List: ruby-talk #362823
Since someone else already offered a solution, I'm going to be a bit critical. 
Sorry about that, but your question alone suggests you really need to learn a 
lot more about how Ruby (or Rails) and JavaScript communicate.

I apologize in advance -- you said you're a newbie -- but this might be harsh. 
I want you to understand why this is wrong, and why it's so fractally wrong, 
before you try to do it right.

On Saturday, May 15, 2010 07:45:09 pm Brandon Jake12jake wrote:
> So I'm very new to both ruby and javascript, but the basic problem is. I
> have a ruby array @events_this_month, and i'm running a javascript loop
> that basically needs the information from this array.

So, think about what's actually going on.

You're probably using Rails. Ruby is not Rails, and this is not a Rails 
mailing list. Even if you're using another Ruby framework, though, it looks 
very much like you are running Ruby on the server side, and JavaScript in a 
web browser.

That means you've got two programs which are not only not in the same 
language, they aren't even on the same machine!

So no, you can't just access the array. First you need to get the data from 
your Ruby script to your JavaScript program, somewhere over the network. Then 
you can worry about how to loop over it.

> The problem is I
> can't seem to set the proper index for my ruby array. What I have looks
> like this:
> 
> 
>   for( var i = 0; i < 3; i++)
>   {
>        var title = '<%= @events_this_month[i].title %>';
>     alert(title);
>   }

First of all, how do you know it's always 3? And why on earth wouldn't you go 
off the length of the array?

But again, try to think about what's happening here. You've shown me what 
looks like a Ruby template. What does a template actually do?

Ruby is going to run through that, and this is the important point, BEFORE it 
gets sent to the client. That means, before your javascript is executed at 
all, Ruby will have finished with that page. That means, Ruby just sees:

a big blob of text
@events_this_month[i].title
another big blob of text

The variable 'i' doesn't exist at this point. Not in Ruby, because it'd be a 
JavaScript variable. Not in JavaScript, because the JavaScript program isn't 
running yet.

Just because two languages are present in the same source file doesn't mean 
they can be mashed together like that.

One more thing: You already have a vulnerability in that. What if someone puts 
an apostrophe in the title?

> I've also tried:
> 
>         <%= $j = 0 %>

Globals are evil. Never use them. You're very carefully using 'var' in 
JavaScript -- why would you use a global variable in Ruby, especially for an 
iterator variable!

>   for( var i = 0; i < 3; i++)
>   {
>        var title = '<%= @events_this_month[$j].title %>';

I can't imagine why you'd expect this to work, either. JavaScript is 
incrementing a variable called i, and Ruby is using a variable called $j. How 
is either language supposed to know they're related?

>     alert('<%= $j %>');
>                 <%= $j = $j +1 %>
>   }

So remember, the for loop is JavaScript. Ruby just sees it as a bunch of text. 
That means Ruby sees this:

TEXT
$j = 0
TEXT
@events_this_month[$j].title
TEXT
$j
TEXT
$j = $j +1
TEXT

Or, if you take out the text, here's what Ruby sees:

$j = 0
@events_this_month[$j].title
$j
$j = $j +1

> Any suggestions on how I can access the correct index in my ruby array
> during the javascript loop?

Nope, that's completely impossible, because you can't access Ruby during the 
JavaScript loop.

Now, the suggested solution will work, but I want you to think about how and 
why it works:


var events = <%= @events_this_month.to_json %>;

for (var i=0; i<events.length; i++)
  alert(events[i].title);


That will convert the entire Ruby array to JavaScript, which means Ruby sees 
this:

TEXT
@events_this_month.to_json   # hey, something I can do!
TEXT

JavaScript will see something like this:

var events = [
  { title: 'First event', time: 'Monday' },
  { title: 'Second event', time: 'Tuseday' },
  { title: 'Third event', time: 'Wednesday' }
];

for (var i=0; i < events.length; i++)
  alert(events[i].title);


Now, if you really only need the title of the event, you could simplify it a 
bit (and save some bandwidth) by only sending that:

var titles = <%= @events_this_month.map(&:title).to_json %>;

for (var i=0; i<titles.length; i++)
  alert(titles[i]);

Think about why that's the case. What will Ruby see? What will JavaScript see?



One more thing: It was painfully obvious this time what your mistake was, but 
in the future, you may want to tell us a little more. "I'm using Rails" would 
be a good start, but the most important thing is, if you ever catch yourself 
writing "It doesn't work," STOP! And tell us what actually happened.

"It doesn't work" is useless. It's like going to the doctor and saying "I feel 
sick." What are your symptoms? What happens, and what did you expect to 
happen? What error messages did you get? There's all kinds of things you can 
say other than "It doesn't work."

In This Thread