Ruby with Mac OS X

Hello everyone,

I have a friend that is having some trouble getting a ruby program to
work on her Mac.

I’m kind of wondering if there is anything she can do, I’ve looked all
over the internet and things, are there any mac users that might be
willing to put in a word of advice?

Thanks.

On Jan 18, 2008 8:37 PM, Tj Superfly [email protected] wrote:

I have a friend that is having some trouble getting a ruby program to
work on her Mac.

I’m kind of wondering if there is anything she can do,

Yep.

are there any mac users that might be willing to put in a word of advice?

Sure, several words: provide some real information about the specific
problem and the environment in which it’s happening… :slight_smile:

She can’t find the run file.

She’s installed “ruby” but it will only open the .rb file in a text
editor.

When she clicks on other (to locate another program) we can’t find
anything ruby related.

Does that help?

On Jan 18, 2008 9:25 PM, Tj Superfly [email protected] wrote:

She can’t find the run file.

She’s installed “ruby” but it will only open the .rb file in a text
editor.

When she clicks on other (to locate another program) we can’t find
anything ruby related.

Does that help?

Not really. What program are you trying to run? What version of
Mac OS and Ruby are installed?

Hassan S. wrote:

On Jan 18, 2008 9:25 PM, Tj Superfly [email protected] wrote:

She can’t find the run file.

She’s installed “ruby” but it will only open the .rb file in a text
editor.

When she clicks on other (to locate another program) we can’t find
anything ruby related.

Does that help?

Not really. What program are you trying to run? What version of
Mac OS and Ruby are installed?

Mac OS 10.5 (leopard)

it said that ruby 1.9.0 is installed

On Jan 18, 2008, at 9:35 PM, Tj Superfly wrote:

Does that help?

Not really. What program are you trying to run? What version of
Mac OS and Ruby are installed?

Mac OS 10.5 (leopard)

it said that ruby 1.9.0 is installed

If you’re expecting some zippy IDE, that’s not really what Ruby is
about. Ruby is a programming language.

Open a terminal window (it’s under Applications:Utilities)

Enter some text in a text editor (emacs, vim, vi, …):

1.upto(3){|n| puts “hello #{n}”}

save it as hello.rb.

type:

ruby hello.rb

results are:

hello 1
hello 2
hello 3

Does this get you started more in the right direction?

Okay, we’ve got programs running!

Thanks for that.

Now, the program I’ve created pulls information from a .yaml file. It
works fine on windows, but it says

“Macintosh: ~Xepha$ ruby /users/Xepha/Documents/for-v/numbered_news.rb
/users/Xepha/Documents/for-v/numbered_news.rb:14: uninitialized constant
YAML (NameError)”

So, all the files are in the correct place, there is data inside of the
files, why won’t YAML files work?

not sure what you are trying to do there…

This might help:

Constants in Ruby
A variable whose name begins with an uppercase letter (A-Z) is a
constant. A constant can be reassigned a value after its
initialization, but doing so will generate a warning. Every class is
a constant.

Trying to substitute the value of a constant or trying to access an
uninitialized constant raises the NameError exception.

perhaps you need to read up a little more about Ruby Syntax?

If you send the code for numbered_news.rb I’m sure someone on this
list can help…

Well, I’m using it with arrays, here’s a clip of the code that I’m
using.

captions_number = YAML.load(File.open(“arrays/captions_number.yaml”))
captions = YAML.load(File.open(“arrays/captions_array.yaml”))
captions_delete = []

Basically that clip right there opens the files and sets them for an
array that I use later in the program. I then at the end, send out all
the updates arrays to the files so when it loops back through the array
is updated. This way I can close the program and my arrays are still set
where they were left off.

Like I said it works great in windows, not sure why it’s not working
with the Mac.

Tj Superfly wrote:

is updated. This way I can close the program and my arrays are still set
where they were left off.

Like I said it works great in windows, not sure why it’s not working
with the Mac.

Ask rather why it’s working on Windows. The yaml library is not
available by default. You have to require it first. Add this to the top
of your script.

require ‘yaml’

require ‘yaml’

seems like you may want to back up and do some more reading…

here’s a good starting point…
http://en.wikibooks.org/wiki/Ruby_Programming

Tj Superfly wrote:

Basically it’s a program that checks to see if images exist. =D Now,
with the proper files and everything, it works wonders on windows.
However, even adding the require ‘yaml’ didn’t work. It was there from
the beginning, so that couldn’t be the problem - unless you have to have
“yaml” on the mac somewhere?!?

yaml is part of the standard Ruby installation. When you require “yaml”
it defines the YAML constant. If the yaml.rb file doesn’t exist for some
reason then you would get a LoadError exception. Since you didn’t
mention such an exception I assume that require “yaml” worked normally
and defined the YAML constant.

In mysterious cases like this I find it helps to simplify things a bit.
Fire up irb and enter these commands:

require “yaml”
p YAML

The output from the 2nd statement should be YAML. If not, your ruby
install is hosed.

I notice from an earlier post that you’re running 1.9.0, which is
basically a test version of Ruby. OS X 10.5 comes with Ruby 1.8.6
installed, so I’m curious why you’re running 1.9.0?

That works fine, the output was “YAML”. From what you said, that’s a
good sign.

Now I’m still getting the same error message that I posted above when I
run the program.

“Macintosh: ~Xepha$ ruby /users/Xepha/Documents/for-v/numbered_news.rb
/users/Xepha/Documents/for-v/numbered_news.rb:14: uninitialized constant
YAML (NameError)”

And, turns our 1.9.0 is the current one installed and running; she
didn’t know that it was preinstalled on macs. =/ So, yes she is running
1.9.0 now; YAML is “working” just not in the program that I’ve written.

Okay, we’ve added that to the mac, and still the same error. I’m going
to post a little bit more of the code, it’s 900+ lines long, so i’m
going to only put part of it, the main part of it is repeating for a
different thing.

///////////////// CODE BEGINING /////////////////

$defout.sync=true
attempt = 0
time = Time.now
day = time.wday
year = time.year

require ‘uri’
require ‘net/http’
require ‘FileUtils’
require ‘date’
require ‘yaml’

//** Captions Infomation **//

captions_number = YAML.load(File.open(“arrays/captions_number.yaml”))
captions = YAML.load(File.open(“arrays/captions_array.yaml”))
captions_delete = []

def image_exists?(url_string)
url = URI.parse(url_string)
Net::HTTP.start(url.host, url.port) do |http|
response = http.head(url.path)
case response
when Net::HTTPSuccess, Net::HTTPRedirection
case response.content_type
when “image/png”, “image/gif”, “image/jpeg”
return true
end
end
end
return false
end

def user_wants_to_save?(url)
print “#{url}”
puts “”
#puts "Time: " + time.to_s
puts "Delete Permanently? [y/N]: "
input = $stdin.gets
if /y/i =~ input
return true
else
return false
end
end

while(attempt >= 0)
time = Time.now
attempt += 1
print “\n”, "------------------- ", attempt.to_s, " - “, time.hour,
“:”, time.min, “:”, time.sec, " -------------------”, “\n”

   # //** Captions **//

captions.each do |url|
if image_exists?(url)
if user_wants_to_save?(url)
captions_delete.push url
end
end
end

captions_delete_number = captions_delete.length
captions_delete_number -= 1

while(captions_delete_number >= 0)
captions.delete(captions_delete[captions_delete_number])
captions_delete.delete(captions_delete[captions_delete_number])
captions_delete_number -= 1
end

while(captions.length < 8)
captions_number += 1
captions.push “http://images.neopets.com/caption/sm_caption_” +
captions_number.to_s + “.gif”
end

File.open(“arrays/captions_array.yaml”,“w”) do |out|
out << captions.to_yaml
end
File.open(“arrays/captions_number.yaml”,“w”) do |out|
out << captions_number.to_yaml
end

//////// END OF CODE ////////////

Basically it’s a program that checks to see if images exist. =D Now,
with the proper files and everything, it works wonders on windows.
However, even adding the require ‘yaml’ didn’t work. It was there from
the beginning, so that couldn’t be the problem - unless you have to have
“yaml” on the mac somewhere?!?

Oh, I forgot to add that I wanted to thank you for working through this
with me and not just telling me to read stuff. I know reading is a great
way to learn, but an even better way is through trial and error, and
these forums have really helped me in the past. =D It’s people like you
who really help people like me.

On 19 janv. 08, at 18:43, Tj Superfly wrote:

And, turns our 1.9.0 is the current one installed and running; she
didn’t know that it was preinstalled on macs.

Ruby 1.9 isn’t preinstalled anywhere, especially not on Macs. The pre-
installed version of Ruby on OS X 10.5 is 1.8.6-p111 for example, so
if you have a 1.9 then someone installed it manually. And that was a
bad idea since 1.9 is a development release.

On Jan 19, 2008 9:43 AM, Tj Superfly [email protected] wrote:

And, turns our 1.9.0 is the current one installed and running; she
didn’t know that it was preinstalled on macs.

That’s very hard to believe. When did she get this system? Because
as of December when I got a new Mac, it came with 1.8.6.

Open a terminal window and type ruby -v and which ruby and
see what they output.

Tj–

I would recommend you reduce your program to the smallest number of
lines of code that fail around this particular problem. If you still
can’t see what is causing this, then put the code on
http://pastie.caboo.se
so everyone can look at it. Remember, looking at your whole program
is going to be more confusing than looking at the most trivial
reproducible case.

Also, as mentioned, you can eliminate some complexity by simply using
Ruby 1.8.6, as shipped with Mac OS X. That way, most people on this
list will have the same release. Certainly some are running 1.9, but
it is a development release so YMMV.

As a previous poster mentioned, you should ask yourself why it works
on Windows. If you type ruby -v on Windows, do you get 1.8.6?

There are a number of things to explore here. Happy spelunking.

On Jan 19, 2008 10:08 AM, Tj Superfly [email protected] wrote:

I have reduced the code to the line where it’s not working, which I
originally posted; then someone else asked for more, so I basically
condensed the whole thing into the 2nd portion of code that I posted.

And if I paste your sample code into irb, down to where it loads the
first flle, it works fine here.

Have you tried stepping it line by line in irb on your system?

Hassan S. wrote:

On Jan 19, 2008 9:43 AM, Tj Superfly [email protected] wrote:

And, turns our 1.9.0 is the current one installed and running; she
didn’t know that it was preinstalled on macs.

That’s very hard to believe. When did she get this system? Because
as of December when I got a new Mac, it came with 1.8.6.

Open a terminal window and type ruby -v and which ruby and
see what they output.

Anyway, it appears from using that to get the version, 1.8.6 is running.

Macintosh:~ Xepha$ ruby -v
ruby 1.8.6 (2007-03-13 patchlevel 0) [universal-darwin8.0]
Macintosh:~ Xepha$

So, :stuck_out_tongue: now that, that is all solved, YAML? =(

Here’s what we know,

  1. YAML is on the system.
  2. So something is wrong with the program code, which I provided above.

I'm running 1.8.6 on my windows also. The versions are identical. This 
YAML thing shouldn't be an issue.

I have reduced the code to the line where it's not working, which I 
originally posted; then someone else asked for more, so I basically 
condensed the whole thing into the 2nd portion of code that I posted.