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
Lamont Cranston
Sep 1, 2006

how do i shot foam
Since this is more or less the Ruby thread, I'll ask this here -
I'm using RSpec but I'm pretty new to it. I'm writing a web crawler and I want to set up a test to ensure that any page is only downloaded once. I have my Crawler class send an update message to my Observer class every time it downloads a page with the response and URI objects as arguments.

My test looks like this:
code:
          uri = URI.parse('http://localhost:12000')
          @obs.should_receive(:update).with(kind_of(Net::HTTPResponse), uri + '/page5.html').once
          @crawler.crawl(uri)
This passes even though I know that page5.html is downloaded twice. If I change .once to .twice, it also passes. If I change to 3 times, I get this:
code:
Mock "observer" received :update with unexpected arguments
  expected: (#<Spec::Mocks::ArgumentMatchers::KindOf:0x1010615d8 @klass=Net::HTTPResponse>, #<URI::HTTP:0x1010600e8 URL:[url]http://localhost:12000/page5.html[/url]>)
       got: ([#<Net::HTTPOK 200 OK  readbody=true>, #<URI::HTTP:0x10105ef68 URL:[url]http://localhost:12000/[/url]>], 
          [#<Net::HTTPOK 200 OK  readbody=true>, #<URI::HTTP:0x101038160 URL:[url]http://localhost:12000/page2.html[/url]>], 
          [#<Net::HTTPOK 200 OK  readbody=true>, #<URI::HTTP:0x101033110 URL:[url]http://localhost:12000/page3.html[/url]>], 
          [#<Net::HTTPNotFound 404 Not Found  readbody=true>, #<URI::HTTP:0x1005b4540 URL:[url]http://localhost:12000/page4.html[/url]>])
Anybody have any idea what I'm doing wrong?

Lamont Cranston fucked around with this message at 23:31 on Jan 24, 2010

Adbot
ADBOT LOVES YOU

Operation Atlas
Dec 17, 2003

Bliss Can Be Bought

Lamont Cranston posted:

This passes even though I know that page5.html is downloaded twice. If I change .once to .twice, it also passes. If I change to 3 times, I get this:
code:
Mock "observer" received :update with unexpected arguments
  expected: (#<Spec::Mocks::ArgumentMatchers::KindOf:0x1010615d8 @klass=Net::HTTPResponse>, #<URI::HTTP:0x1010600e8 URL:[url]http://localhost:12000/page5.html[/url]>)
       got: ([#<Net::HTTPOK 200 OK  readbody=true>, #<URI::HTTP:0x10105ef68 URL:[url]http://localhost:12000/[/url]>], 
          [#<Net::HTTPOK 200 OK  readbody=true>, #<URI::HTTP:0x101038160 URL:[url]http://localhost:12000/page2.html[/url]>], 
          [#<Net::HTTPOK 200 OK  readbody=true>, #<URI::HTTP:0x101033110 URL:[url]http://localhost:12000/page3.html[/url]>], 
          [#<Net::HTTPNotFound 404 Not Found  readbody=true>, #<URI::HTTP:0x1005b4540 URL:[url]http://localhost:12000/page4.html[/url]>])
Anybody have any idea what I'm doing wrong?

It's hard to tell without looking at the code in your app, but I think you're passing every page url in an array each time you talk to the observer. Each time you hit a new page, the array grows and so does the list of pages passed to the observer. Because of the way RSpec does "with", as long as the page is in that array it will pass.

Lamont Cranston
Sep 1, 2006

how do i shot foam

Operation Atlas posted:

It's hard to tell without looking at the code in your app, but I think you're passing every page url in an array each time you talk to the observer. Each time you hit a new page, the array grows and so does the list of pages passed to the observer. Because of the way RSpec does "with", as long as the page is in that array it will pass.

It just passes the URI object, no array. I managed to get it to (correctly) fail when I change to:
code:
@obs.should_receive(:update).with(kind_of(Net::HTTPResponse), uri + '/page5.html').once
@obs.should_not_receive(:update).with(kind_of(Net::HTTPResponse), uri + '/page5.html')
but this doesn't seem like it's how it should have to work.

Operation Atlas
Dec 17, 2003

Bliss Can Be Bought

Lamont Cranston posted:

It just passes the URI object, no array. I managed to get it to (correctly) fail when I change to:
code:
@obs.should_receive(:update).with(kind_of(Net::HTTPResponse), uri + '/page5.html').once
@obs.should_not_receive(:update).with(kind_of(Net::HTTPResponse), uri + '/page5.html')
but this doesn't seem like it's how it should have to work.

Well, dude, I don't know what to tell you, but it is definitely passing an array.

Pardot
Jul 25, 2001




Operation Atlas posted:

Well, dude, I don't know what to tell you, but it is definitely passing an array.

Or, I guess, the uri object's #to_s could print like an array. But that's unlikely and you're probably right.

Lamont Cranston
Sep 1, 2006

how do i shot foam
http://github.com/tylercunnion/crawler/blob/master/lib/crawler/webcrawler.rb

It's not passing an array, have a look for yourselves.

At any rate, I think the problem I'm having has to do with what they discuss in this ticket:
https://rspec.lighthouseapp.com/projects/5645/tickets/618-exactlyntimes-incorrectly-failing-for-n-actual

I stub the update method onto my observer object, because for the Observable module, observer classes must respond to update. Then, I put an expectation on top of that - and when it's finished, it removes the expectation and reverts back to the stub method - which is why it doesn't catch that the method is being called more than what I specify, but it will catch it when I put the expectation to a high number I know it won't reach.

Anyway, thanks for your help. I guess I'll just have to continue to work around or else figure out something clever.

Operation Atlas
Dec 17, 2003

Bliss Can Be Bought

Lamont Cranston posted:

http://github.com/tylercunnion/crawler/blob/master/lib/crawler/webcrawler.rb

It's not passing an array, have a look for yourselves.

At any rate, I think the problem I'm having has to do with what they discuss in this ticket:
https://rspec.lighthouseapp.com/projects/5645/tickets/618-exactlyntimes-incorrectly-failing-for-n-actual

I stub the update method onto my observer object, because for the Observable module, observer classes must respond to update. Then, I put an expectation on top of that - and when it's finished, it removes the expectation and reverts back to the stub method - which is why it doesn't catch that the method is being called more than what I specify, but it will catch it when I put the expectation to a high number I know it won't reach.

Anyway, thanks for your help. I guess I'll just have to continue to work around or else figure out something clever.

Ok, so try using at_most(1).times.

Also, try calling delete_observers before add_observer. There might be some weird poo poo going on with multiple observers getting attached.

Operation Atlas fucked around with this message at 08:50 on Jan 27, 2010

hmm yes
Dec 2, 2000
College Slice
I'm about to convert a rails app to support mobile devices--no specific type. Other than busting out mobile_fu, optimizing layouts, js, and css... anyone have any recommendations or gotchas that they experienced doing the same thing?

wolffenstein
Aug 2, 2002
 
Pork Pro
Until you can replicate every option and every feature from the normal interface into the mobile interface, always give the mobile user an option to use the normal website. I can't recall how many times I've encountered not being able to do what I want on a mobile site, and then I wouldn't be able to switch to the normal site to do what I want. Easiest way to lose mobile visitors.

plasticbugs
Dec 13, 2006

Special Batman and Robin
I stupidly upgraded my Ruby install on OS X Snow Leopard to 1.9.1 while following a new Rails book tutorial. To my credit, I followed the directions word-for-word, thinking what a good idea it would be to have the latest version of Rails AND the latest stable version of Ruby, too.

Now WEBrick crashes like a champ (EDIT: mongrel fails to build, too!) - obviously Rails isn't ready for Ruby 1.9.1. Is there an easy way to downgrade my Ruby back to 1.8.7?

EDIT 3: Fixed my code and WEBrick is still crashing.

My Ruby is in:
/usr/local/bin/ruby

Also, if you are gracious enough to help me, please assume that I'm a complete idiot.

EDIT 4: Holy poo poo. After an hour of wrestling with installing and uninstalling Ruby, Gems and Rails, my ultimate solution was to TRASH my Ruby binary, my Rails binaries (rake, heroku et al.) AND my entire Gem folder (my 1.8 AND my 1.9 gems) - every single Gem. I reinstalled Ruby 1.8.7, reinstalled RubyGems and all my gems including Rails.

It works. Lesson learned. Don't do what I did.

plasticbugs fucked around with this message at 09:44 on Feb 3, 2010

Ghotli
Dec 31, 2005

what am art?
art am what?
what.
Rails 3.0 is not out, but the release notes have been written. They outline most of the changes we will see in 3.0, how they work, and why they were changed.

http://guides.rails.info/3_0_release_notes.html

NotShadowStar
Sep 20, 2000

plasticbugs posted:

I stupidly upgraded my Ruby install on OS X Snow Leopard to 1.9.1 while following a new Rails book tutorial. To my credit, I followed the directions word-for-word, thinking what a good idea it would be to have the latest version of Rails AND the latest stable version of Ruby, too.

Now WEBrick crashes like a champ (EDIT: mongrel fails to build, too!) - obviously Rails isn't ready for Ruby 1.9.1. Is there an easy way to downgrade my Ruby back to 1.8.7?

EDIT 3: Fixed my code and WEBrick is still crashing.

My Ruby is in:
/usr/local/bin/ruby

Also, if you are gracious enough to help me, please assume that I'm a complete idiot.

EDIT 4: Holy poo poo. After an hour of wrestling with installing and uninstalling Ruby, Gems and Rails, my ultimate solution was to TRASH my Ruby binary, my Rails binaries (rake, heroku et al.) AND my entire Gem folder (my 1.8 AND my 1.9 gems) - every single Gem. I reinstalled Ruby 1.8.7, reinstalled RubyGems and all my gems including Rails.

It works. Lesson learned. Don't do what I did.

Use rvm. I've come to love it like a gay go-to bottom that's always willing. It works even without a system Ruby.

Pardot
Jul 25, 2001




NotShadowStar posted:

Use rvm. I've come to love it like a gay go-to bottom that's always willing. It works even without a system Ruby.

This is the best review of a gem ever.

plasticbugs
Dec 13, 2006

Special Batman and Robin

NotShadowStar posted:

Use rvm. I've come to love it like a gay go-to bottom that's always willing. It works even without a system Ruby.

Halfway through my process I found rvm, but I was well past the point of no return. I didn't realize the amount of things that were about to break as I "sudo rm'd" Rails binaries one-by-one from /usr/local.

I will begin using rvm to avoid this nonsense with future updates.

jonnii
Dec 29, 2002
god dances in the face of the jews

NotShadowStar posted:

Use rvm. I've come to love it like a gay go-to bottom that's always willing. It works even without a system Ruby.

RVM really is totally fantastic. I recommend it to EVERYBODY. If you aren't using it you should start using it right now.

NotShadowStar
Sep 20, 2000
Seriously rvm is so erection worthy I hope other languages copy it. Especially Perl, loving CPAN is a goddamn nightmare.

I even have a legacy Ruby CGI app I have floating around still that I got working with rvm easily. I put #/path/to/user/.rvm/bin/passenger_ruby and it runs the same rvm setup routine that Passenger uses to setup the correct Ruby and Gem versions and that's it.

Come here you little mustached leather stud, lets go another round.

wolffenstein
Aug 2, 2002
 
Pork Pro
I'm really curious how you found out about rvm.

Pardot
Jul 25, 2001




wolffenstein posted:

I'm really curious how you found out about rvm.

Well NSS found out about it on the last page ;)

I found out about it since I'm an RSS/podcast whore.

bitprophet
Jul 22, 2004
Taco Defender
Does RVM provide anything other than one "bucket" per Ruby version/implementation? I.e. how exactly does it compare to Rip or (Rip's inspiration) Python's virtualenv?

It sounds like that it makes it easy to switch the default Ruby interpreter/loadpath/gempath between e.g. 1.8.6/1.8.7/JRuby/etc...but what if you wanted to have a couple different projects all using e.g. 1.8.6 but each with different sets of gems/libs? (Which is the use case for Rip/virtualenv -- each project can have a "clean" set of packages instead of clobbering each other's dependencies or dealing with multiple versions of the same gem.)

It still sounds like a rockin' way to achieve the base/obvious use case, though.

wolffenstein
Aug 2, 2002
 
Pork Pro

Pardot posted:

Well NSS found out about it on the last page ;)

I found out about it since I'm an RSS/podcast whore.
Your simple and factual story is boring, and thus I don't believe you.

jonnii
Dec 29, 2002
god dances in the face of the jews

wolffenstein posted:

I'm really curious how you found out about rvm.

I think I found about it from the rails envy podcast. Now I answer all gem related questions on stackoverflow with RVM propaganda in the hope that it gets more traction.

NotShadowStar
Sep 20, 2000

bitprophet posted:

Does RVM provide anything other than one "bucket" per Ruby version/implementation? I.e. how exactly does it compare to Rip or (Rip's inspiration) Python's virtualenv?

It sounds like that it makes it easy to switch the default Ruby interpreter/loadpath/gempath between e.g. 1.8.6/1.8.7/JRuby/etc...but what if you wanted to have a couple different projects all using e.g. 1.8.6 but each with different sets of gems/libs? (Which is the use case for Rip/virtualenv -- each project can have a "clean" set of packages instead of clobbering each other's dependencies or dealing with multiple versions of the same gem.)

It still sounds like a rockin' way to achieve the base/obvious use case, though.

You read the goddamn documentation. Then lube up because you're in for a fun time, and you're the dominant man entirely in control while the freshly shaven studmuffin is rear end up ready to take your orders.

drjrock_obgyn
Oct 11, 2003

once i started putting pebbles in her vagina, there was no going back

jonnii posted:

I think I found about it from the rails envy podcast. Now I answer all gem related questions on stackoverflow with RVM propaganda in the hope that it gets more traction.

I found out about it from the internet. But seriously I found out about it from http://delicious.com/popular/ruby -- I have it set up in RSS menu.

bitprophet
Jul 22, 2004
Taco Defender

NotShadowStar posted:

You read the goddamn documentation.

A Ruby project with documentation? :monocle: (It actually looks quite thorough, for a change. Nice. I guess I'll...read it! And then decide if I want to manhandle it.)

EDIT: According to the gemsets page it does indeed have mechanisms for multiple "environments" per interpreter. Cool beans. Will definitely have to look into it, I've been pining for a virtualenv-a-like, and tried out Rip previously which was too unstable.

bitprophet fucked around with this message at 02:44 on Feb 4, 2010

deadjb
Aug 11, 2002

I'm sorry, this isn't a rails question. It's a ruby question, but I don't have the guts to start a general ruby thread. I can if there's a want/need for it, though.

I've got an array of Edge objects. Edge is defined as such, basically:

code:
class Edge
    attr_reader :p1, :p2
end
where p1 and p2 are 2d Vectors defining the points of said Edge.

I want to get a Vector with the smallest x and y coordinates across all the Edges' points. To be clear, the x component can come from a different Edge than the y component.

I'm doing this like this currently:

code:
minxe = (edges.min{|e1, e2| e1.p1[0] <=> e2.p1[0]}) #[0] is x, [1] is y
minVX = [minxe.p1[0], minxe.p2[0]].min
(...)
minV = Vector[minVX, minVY]
but it feels wrong, like there is a more elegant way of doing this in ruby. I'm always fine with "if it ain't broke, don't fix it", but since I haven't written in ruby in a long while, I wanted to take this as a learning opportunity. Any suggestions?

Pardot
Jul 25, 2001




my setup:
code:
require 'matrix'
class V < Vector; end

class Edge
    def self.[](p1,p2)
      self.new(p1,p2)
    end
    def initialize(p1,p2)
      @p1, @p2 = p1, p2
    end
    attr_reader :p1, :p2
end


edges = []
edges << Edge[V[1,8], V[5,4]] << Edge[V[4,6], V[3,0]] << Edge[V[4,3], V[5,1]]
If you don't want to modify Edge:
code:
xy = edges.map {|e| [[e.p1[0], e.p2[0]].min, [e.p1[1], e.p2[1]].min] }.transpose
V[xy[0].min, xy[1].min] # => Vector[1, 0]
That's still ugly though, so if you want to add some things to Edge

code:
class Edge
  def min_x
    [ @p1[0], @p2[0] ].min
  end
  def min_y
    [ @p1[1], @p2[1] ].min
  end
end

V[ edges.map{|e| e.min_x}.min, edges.map{|e| e.min_y}.min ] # => Vector[1, 0]
That's not so bad.

deadjb
Aug 11, 2002

Thank you so much! I feel like an idiot not thinking of that. This is a huge help. :)

bitprophet
Jul 22, 2004
Taco Defender
Not a huge Rails user, but I have been following Rails 3 with some interest -- Katz seems like a heck of a guy, and they're clearly drawing some inspiration from e.g. Django, which can only lead to good things.

So I'm glad to see that not only were those pre-release release notes published, but the Rails 3 beta is out now :)

Splinter
Jul 4, 2003
Cowabunga!
Anyone have any recommendations for graphing packages? I'm looking for something that can dynamically generate graphs using database data.

NotShadowStar
Sep 20, 2000
I went down that route and decided that they all suck rear end. Most of them depend on RMagick which is loving terrible.

What I ended up doing is generating json data (easy with standard records doing to_json) and using flot to display it and give the user more control over the graph. Only problem is since IE is loving terrible and doesn't support canvas flot uses a VRML JS wrapper, and IE's JS performance is such poo poo it can take unreasonably long. About 8-10 seconds on a modern system with 384 points of data, whereas Firefox >3.5 and any Webkit is instant.

Hammertime
May 21, 2003
stop

NotShadowStar posted:

I went down that route and decided that they all suck rear end. Most of them depend on RMagick which is loving terrible.

What I ended up doing is generating json data (easy with standard records doing to_json) and using flot to display it and give the user more control over the graph. Only problem is since IE is loving terrible and doesn't support canvas flot uses a VRML JS wrapper, and IE's JS performance is such poo poo it can take unreasonably long. About 8-10 seconds on a modern system with 384 points of data, whereas Firefox >3.5 and any Webkit is instant.

I've had to do some rails graphing again and I've had the same experience, gently caress Scruffy/Gruff etc. We've started using flot (and failing that I probably would have gone back to google charts again), but again we don't need to support IE for this app since it's internal.

Operation Atlas
Dec 17, 2003

Bliss Can Be Bought

Splinter posted:

Anyone have any recommendations for graphing packages? I'm looking for something that can dynamically generate graphs using database data.

I've been using Highcharts and it works quite well. Sure, it's Javascript, but it beats the hell out of using RMagick.

Ghotli
Dec 31, 2005

what am art?
art am what?
what.
Just chiming in. We use flot as well. I've come to the conclusion that if they're looking at my charts in IE6 then gently caress them. They're getting what they deserved.

Hammertime
May 21, 2003
stop

Ghotli posted:

Just chiming in. We use flot as well. I've come to the conclusion that if they're looking at my charts in IE6 then gently caress them. They're getting what they deserved.

Amen, http://emotify.com/acy149/

Splinter
Jul 4, 2003
Cowabunga!
Thanks for the recommendations. I'll be giving flot a try. Does anyone have experience using any flot related gems? Are they any good? I saw there was one called flotilla, but it doesn't look like its been added to gemcutter yet.

NotShadowStar
Sep 20, 2000
There really is no need for some sort of gem since all the stuff in Rails makes it straightforward. All you require from Rails is your data in JSON, which is easy. The rest is all building a UI around Flot and that's all view work.

NotShadowStar
Sep 20, 2000
This isn't Rails-specific, but hey, whatev.

I've been fixing up Net::LDAP because it needs a LOT of love. I've managed to get the file structure sane so it loads according to general Ruby principles and works fine in Rails 3, and with the help of some Swiss guy got it to at least function in 1.9. It does what I need to, but it needs quite a bit more love to make it modern and so I feel comfortable releasing it, the original developers seemed to have dropped off the planet.

Something that needs to happen is comprehensive communication tests. Right now the test coverage just makes sure that encodes and decodes data properly. What I want to do is add tests for different LDAP servers, but that's impossible as-is without actually having different types of LDAP servers (AD, OpenDirectory etcetra) which is impossible to replicate. As an idea, would it be a bad idea making a mock object that mocks the relevant IO and SSL objects as needed, capture the data from a real LDAP server, then use the mock object to contain that data? Such as

code:
describe Net::LDAP do
  it "should get a test user from Active Directory" do
    # mock the IO and SSL connection objects here that .connect relies on
    
    ldap = Net::LDAP.connect()
    # bind and search like you would
  end
end

dustgun
Jun 20, 2004

And then the doorbell would ring and the next santa would come
I think that as an addition to the encoding checks, that makes a lot of sense. Just include a readme explaining what you did and that a failure in one of those tests shouldn't imply that it absolutely for sure won't work.

LordNova
Apr 17, 2005
Anyone else going to the LA Ruby conference this weekend?
http://www.larubyconf.com/

Adbot
ADBOT LOVES YOU

keep it down up there!
Jun 22, 2006

How's it goin' eh?

I asked recently about getting RoR setup on windows for some development.
Well since Instant Rails is boat loads behind, and one can assume version 3 wont be ported to it anytime soon, Im thinking of setting up a VM with Linux to do my work.

So any suggestions on what to use? A friend told me to try Ubuntu, this a good idea?
Also any good guides for setting up RoR after that's all setup?

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