Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Soup in a Bag
Dec 4, 2009

Jam2 posted:

I need to create a key for the first_letter. It doesn't exist when I do
code:
letters_and_words = Hash.new([])
Am I missing something?
Read up on Hash.new in the core docs. If you give the Hash a default object or block, that object will be used or that block evaluated whenever you reference a non-existent key.

Unfortunately there can be a problem using a default object:
code:
>> h = Hash.new([])
=> {}
>> h[:foo] # :foo doesn't exist as a key
=> []      # so the single default object is used
>> h[:bar] << 7
=> [7]     # same as above, the default object is used
>> h[:foo]
=> [7]
Instead you'd use the default block form:
code:
>> h = Hash.new {|hash, key| hash[key] = []}
=> {}
>> h[:foo] # default block is evaluated since :foo didn't already exist
=> []
>> h[:bar] << 7
=> [7]
>> h[:foo]
=> []
With the block form of Hash initialization, the body of Enumerable#each_group_by_first_letter can be written in three lines while still being very understandable.

Soup in a Bag fucked around with this message at 05:08 on Jun 17, 2011

Adbot
ADBOT LOVES YOU

Soup in a Bag
Dec 4, 2009

Defghanistan posted:

Well I actually am choking on a ruby error, since chef is checking the .rb file against ruby syntax- so the powershell part of it is moot right now aside from the fact that it has to pass ruby syntax muster. I'll look into that, thanks.

I am sorry for the spam, but I cannot seem to find the error here. I double quoted and escaped the slashes, but now can't find any problems.

http://codepad.org/AgwDrLY5

You're still getting the same error? I'm guessing that this: "invalid multibyte char (US-ASCII)" is the important clue. Just looking at line 104 in your first paste (line 10 in your latest paste), it seems like the '-' in "-Type Directory" is bigger than your other '-'s. Maybe that's what it's complaining about?

Soup in a Bag
Dec 4, 2009
There's String#empty?, but no NilClass#empty? method. If r.email is nil rather than an empty String you'll get that error.

Soup in a Bag
Dec 4, 2009

Juz posted:

I'm trying to use that function to return the contents of a file from the FTP server into a variable rather than write it to a file. The comment for the function says "If localfile is nil, returns retrieved data" and it looks from the source like the intention is there to be able to do this, but I can't see how you can do this given that it sets localFile = File.basename(remotefile) in the argument list.

I thought I might be able to do gettextfile(my_remotefile, nil), but ruby complains about converting "nil" to a string.

Which version of Ruby are you using? That documentation link from your earlier post is for 1.9. In 1.8, #gettextfile doesn't have the option of not writing to a file and passing nil would result in the error you're getting.

e: But in 1.9 what you're attempting would work.

Soup in a Bag fucked around with this message at 17:25 on Sep 7, 2011

Soup in a Bag
Dec 4, 2009
Are you calling include inside a method definition? I don't think you can do that. It should work at the top level or as part of a Module/Class definition though.

But you don't have to use include. For example, if your TestModule contains a class called TestClass, you could use either:
code:
require 'lib/test_module'
obj = TestModule::TestClass.new
or:
code:
require 'lib/test_module'
include TestModule
obj = TestClass.new

Soup in a Bag
Dec 4, 2009
It's been a while since I've played with Shoes, but everything has show/hide/toggle methods for visibility and a remove method. Slots also have a clear method to remove their contents. The manual covers all that and more.

Soup in a Bag
Dec 4, 2009

SquadronROE posted:

I'm learning Ruby right now, going through the Koans and I'm trying to create a method for one of the exercises. Right now, though, I'm stuck on trying to match up a set of numbers to an array.

Bottom line: Does .include? work if you pass an array to it? Will it then look inside a given array to see if the things are in there?

Nope. It will look for the object it's passed (in this case the entire array) and that's it.

Ruby code:
> [1, 1, 1, 1].include?([1, 1, 1])
=> false

> [[1, 1, 1], 1].include?([1, 1, 1])
=> true
The online Ruby docs are pretty handy for these sorts of questions.

Soup in a Bag
Dec 4, 2009
If you want to read the entire stream in and then use it as if it were a file, check out StringIO. It responds to #rewind and all the other File/IO-ish methods.

Adbot
ADBOT LOVES YOU

Soup in a Bag
Dec 4, 2009
Peristalsis, ssh has NumberOfPasswordPrompts as a config option (see man ssh_config). I don't know if Net::SSH gives access to those. On the command line it'd be
code:
$ ssh -o "NumberOfPasswordPrompts 1" user@host

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply