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
Pardot
Jul 25, 2001




Soup in a Bag posted:

Unfortunately there can be a problem using a default object:

Oh yeah, my bad :o: The last several times I used that feature, I had 0 as the default object, which doesn't have that problem.

Pardot fucked around with this message at 05:20 on Jun 17, 2011

Adbot
ADBOT LOVES YOU

enki42
Jun 11, 2001
#ATMLIVESMATTER

Put this Nazi-lover on ignore immediately!

Dooey posted:

My irb stopper working :(

irb(main):001:0> reload!
NoMethodError: undefined method `reload!' for main:Object
from (irb):1
from C:/Ruby192/bin/irb:12:in `<main>'
irb(main):002:0>

Help please? (On win 7)

Isn't reload! particular to the Rails console? I don't think vanilla irb has anything like that.

Obsurveyor
Jan 10, 2003

enki42 posted:

Isn't reload! particular to the Rails console? I don't think vanilla irb has anything like that.
Yep

code:
~/.vim]$ irb
ruby-1.9.2-p180 :001 > reload!
NoMethodError: undefined method `reload!' for main:Object
        from (irb):1
        from /home/ted/.rvm/rubies/ruby-1.9.2-p180/bin/irb:16:in `<main>'
ruby-1.9.2-p180 :002 >

Jam2
Jan 15, 2008

With Energy For Mayhem
How do I add a method at runtime?

I created a new class (Adder) and a constructor that takes a single integer argument. I added a method named missing_method.

When I invoke object.plus10 on an object of the class, I need my code to add a method for plus10 (generally, plusnum for any number) at run time within missing_method.

1) How do I discuss the literal contents of the missing method in Ruby so I can examine it and then create a method from it?

2) My class takes an argument such that the argument ought to become the "identity"(?) of the object (x = Adder.new(10) creates an integer-like object (x => 10)). I got this far (below). How do I make it so that x => 10?
code:
class Person
  attr_accessor :i # Defines accessor method for symbol parameter
  
  def initialize(i)
    @i = i
  end
  
  def method_missing
  end
end

Defghanistan
Feb 9, 2010

2base2furious
Good morning, I am working on a ruby file that calls a powershell interpreter and when I try to upload the file I am receiving the following error:
(using Chef for server automation)

FATAL: Cookbook file recipes/create_bin.rb has a ruby syntax error:
FATAL: /home/noc/chef-repository/.chef/../cookbooks/powershell/recipes/create_bin.rb:104: invalid multibyte char (US-ASCII)
FATAL: /home/noc/chef-repository/.chef/../cookbooks/powershell/recipes/create_bin.rb:103: syntax error, unexpected $end, expecting tSTRING_CONTENT or tSTRING_DBEG or tSTRING_DVAR or tSTRING_END


Here is the code that contains lines 103 and 104:

********
powershell "New-Item" do
#Create a directory and assign permissions
code <<-EOH
$new-item C:\bin –Type Directory
$get-acl C:\bin | Format-List
$acl = Get-Acl C:\bin
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule("Administrators","FullControl", "ContainerInherit, ObjectInherit", "None", "Allow")
$acl.AddAccessRule($rule)
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule("chef","FullControl", "ContainerInherit, ObjectInherit", "None", "Allow")
$acl.AddAccessRule($rule)
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule("Users","Read", "ContainerInherit, ObjectInherit", "None", "Allow")
$acl.AddAccessRule($rule)
Set-Acl c:\bin $acl
Get-Acl c:\bin | Format-List
EOH
end
**********

and here is the link to the entire doc

http://codepad.org/SZZqzV43

Can anyone tell me why I am getting a syntax error here? I can't figure it out. =(

Obsurveyor
Jan 10, 2003

quote:

and here is the link to the entire doc

http://codepad.org/SZZqzV43

Can anyone tell me why I am getting a syntax error here? I can't figure it out. =(
You have double quoted strings with unescaped slashes, assuming powershell works like most languages.

Defghanistan
Feb 9, 2010

2base2furious
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

Defghanistan fucked around with this message at 18:09 on Jun 17, 2011

skidooer
Aug 6, 2001

Jam2 posted:

How do I add a method at runtime?

This is something that really should be used sparingly, but for the sake of learning:

code:
def method_missing(method, *args, &block)
  if value = method.to_s[/plus(\d+)/, 1]
    plus(value.to_i)
    self
  else
    super(method, *args, &block)
  end
end
And to generate the methods after they have been called so that method_missing is only hit once:

code:
def method_missing(method, *args, &block)
  if value = method.to_s[/plus(\d+)/, 1]
    self.class.send(:class_eval) do
      define_method(method) do
        plus(value.to_i)
        self
      end
    end
    
    send(method, *args, &block)
  else
    super(method, *args, &block)
  end
end

Obsurveyor
Jan 10, 2003

Defghanistan posted:

http://codepad.org/AgwDrLY5
What is the new error then? Your new paste is much shorter now.

edit: Oh, I see, I am not familiar with codepad. The error is pretty straightforward, you are missing the require that provides the powershell method.

Obsurveyor fucked around with this message at 20:28 on Jun 17, 2011

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?

Dooey
Jun 30, 2009

enki42 posted:

Isn't reload! particular to the Rails console? I don't think vanilla irb has anything like that.

I did not realize they were different. Thank you.

bitprophet
Jul 22, 2004
Taco Defender

Dooey posted:

I did not realize they were different.

And so it goes.

Still wonder where Ruby would be today without DHH turning it into a slightly more expressive PHP...:sigh:

My guess: much less popular, but also much less pigeonholed and overshadowed by a single framework. Meh.

Obsurveyor
Jan 10, 2003

bitprophet posted:

Still wonder where Ruby would be today without DHH turning it into a slightly more expressive PHP...:sigh:

My guess: much less popular, but also much less pigeonholed and overshadowed by a single framework. Meh.
Care to expand on that or are you going to leave it at vague musings? PHP has fundamental issues with its API, point blank. It is driven by a company that wants to sell you an "optimizer" to fix the purposefully broken performance. The language is over a decade of cruft and they will not let any of it go for fear of breaking things, except every major release breaks tons of stuff. It has to be driven by schizophrenics.

The language at its core is rotten, Ruby is not(and has nothing to do with Rails, really). If you hate Rails, use Sinatra and roll your own poo poo all day, every day.

Obsurveyor fucked around with this message at 05:00 on Jun 18, 2011

Mudoubleha
May 20, 2001
I have no title and I must scream.
So I ran the update for Rails on my Mac OSX and realized that there's really something wrong with the "rails" command whenever I get it typed.

Following the tutorial on the Ruby on Rails site,

Typing "rails new blog" gets me a directory called "new" instead of "blog".

Typing "rails server" populates my server directory with files instead of starting the server.

Anyway to fix this? I have to use ./script/<command> to now to get things done.

Pardot
Jul 25, 2001




Mudoubleha posted:

So I ran the update for Rails on my Mac OSX and realized that there's really something wrong with the "rails" command whenever I get it typed.

Following the tutorial on the Ruby on Rails site,

Typing "rails new blog" gets me a directory called "new" instead of "blog".

Typing "rails server" populates my server directory with files instead of starting the server.

Anyway to fix this? I have to use ./script/<command> to now to get things done.

It seems like your rails did not actually get updated to 3 for whatever reason. Also I'm assuming you're still using the ruby that came with osx, so while you're taking the time to get up to date and all that, you should

1) install rvm https://rvm.beginrescueend.com/
2) rvm install 1.9.2
3) rvm --default use 1.9.2
4) gem install rails

bitprophet
Jul 22, 2004
Taco Defender

Obsurveyor posted:

Care to expand on that or are you going to leave it at vague musings?

I meant more about how both are essentially consigned to be Web-only languages, except Ruby is actually an excellent general-purpose language but still gets pigeonholed into "lol Rails" / "lol Web" because of the inordinate influence Rails has on the Ruby ecosystem.

So you end up with a flood of coders who only know the Rails DSL and don't even realize there is a distinct, not-Web-only language underneath, and I think that's a real shame given how great a language Ruby is.

This isn't a 100% true thing, of course -- look at the CLI and server related tools that have come out of Ruby over the last few years -- but it still grates that every Ruby discussion is full of folks e.g. assuming you have ActiveSupport installed, or pointing you to Rails plugins for X when you wanted a generic solution for X instead.

By comparison, in the Python world, only the newbiest of newbies confuse Django for Python, and even though Django does bring a number of new Python coders into the fold, it doesn't dominate the landscape like Rails does.

Doc Hawkins
Jun 15, 2010

Dashing? But I'm not even moving!


It seems like a nice problem to have.

Jam2
Jan 15, 2008

With Energy For Mayhem
code:
  def method_missing(method)
    if value = method.to_s[/plus(\d+)/, 1]
      puts value, "yes"
    end
  end
2) What is the purpose of the 1 after the comma?

Jam2 fucked around with this message at 08:26 on Jun 19, 2011

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

Soup in a Bag posted:

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:

Instead you'd use the default block form:

This needs further explanation because it's really confusing when you start programming ruby:

code:
ruby-1.9.2-head > a = Hash.new([])
 => {} 
ruby-1.9.2-head > a[:foo] << "wtf"
 => ["wtf"] 
ruby-1.9.2-head > a[:bar] << "really?"
 => ["wtf", "really?"] 
ruby-1.9.2-head > a[:bar]
 => ["wtf", "really?"] 
ruby-1.9.2-head > a[:foo].object_id
 => 2168798500 
ruby-1.9.2-head > a[:bar].object_id
 => 2168798500 
Giving an empty array to Hash.new() doesn't mean that you'll get a new, empty array every time you access an undefined key in the hash, it means that each undefined key in the hash will get a reference to the same array. However, if you do something like what Soup In a Bag says then you get what you'd expect:

code:
ruby-1.9.2-head > b = Hash.new { Array.new }
 => {} 
ruby-1.9.2-head > b[:foo] = "oh"
 => "oh" 
ruby-1.9.2-head > b[:bar] = "ok."
 => "ok." 
ruby-1.9.2-head > b[:foo].object_id
 => 2165102360 
ruby-1.9.2-head > b[:bar].object_id
 => 2165094280 
The first form says "hey, I'm creating an object that can be used by any new key." The second form says "hey, every time a new key is created run this code that creates a new, empty Array object for it." This is even more confusing because the following works exactly like you'd think:

code:
ruby-1.9.2-head > c = Hash.new(0)
 => {} 
ruby-1.9.2-head > c[:foo] += 1
 => 1 
ruby-1.9.2-head > c[:bar] += 1
 => 1 
The difference is that with Hash.new(0) the '0' is considered a literal, not a reference. And it's not that numbers are magic, look:

code:
ruby-1.9.2-head > d = Hash.new('oh?')
 => {} 
ruby-1.9.2-head > d[:foo] += " really?"
 => "oh? really?" 
ruby-1.9.2-head > d[:bar] += " I guess I get it?"
 => "oh? I guess I get it?" 
The principle of "least surprise" unfortunately for newcomers means "least surprise to people who know the language well." This is actually a good thing, though, since Ruby does behave as you'll expect, just not right off the bat.

Mudoubleha
May 20, 2001
I have no title and I must scream.

Pardot posted:

It seems like your rails did not actually get updated to 3 for whatever reason. Also I'm assuming you're still using the ruby that came with osx, so while you're taking the time to get up to date and all that, you should

1) install rvm https://rvm.beginrescueend.com/
2) rvm install 1.9.2
3) rvm --default use 1.9.2
4) gem install rails

Thanks a lot! Managed to get it running now.

xtal
Jan 9, 2011

by Fluffdaddy

Jam2 posted:

code:
  def method_missing(method)
    if value = method.to_s[/plus(\d+)/, 1]
      puts value, "yes"
    end
  end
2) What is the purpose of the 1 after the comma?

It's the second argument to String#[]:

quote:

If a Regexp is supplied, the matching portion of str is returned. If a numeric or name parameter follows the regular expression, that component of the MatchData is returned instead.

xtal fucked around with this message at 08:44 on Jun 19, 2011

kitten smoothie
Dec 29, 2001

What's the best way to handle file uploads these days in Rails 3? I want to be able to upload a ZIP file, unpack it, then do stuff with the contents.

hmm yes
Dec 2, 2000
College Slice
I've used paperclip reliably for many months now, but CarrierWave is an up and coming gem that is supposed to be good. I haven't tried CarrierWave, but will recommend paperclip--it just works. If you're going to be hosted on Heroku and dealing with files >5mb you'll need to use a direct to s3 uploader like this fork of s3-swf-upload because Heroku will kill any long processes like a paperclip upload.

I've worked with rubyzip for on the fly compression and had no complaints about it.

Pardot
Jul 25, 2001




atastypie posted:

I've used paperclip reliably for many months now, but CarrierWave is an up and coming gem that is supposed to be good. I haven't tried CarrierWave, but will recommend paperclip--it just works. If you're going to be hosted on Heroku and dealing with files >5mb you'll need to use a direct to s3 uploader like this fork of s3-swf-upload because Heroku will kill any long processes like a paperclip upload.

I've worked with rubyzip for on the fly compression and had no complaints about it.

It's not so much of the process getting killed (I've had workers go for 16+ hours, though I really don't recommend that), direct to s3 is a better idea no matter where you're hosted because it won't tie up a web processes preventing requests while something is uploading. I've seen ways to get around that by having nginx or apache handele the upload and not your app, but they always seemed like a lot more effort than the direct to s3 things.

kitten smoothie
Dec 29, 2001

atastypie posted:

I've used paperclip reliably for many months now, but CarrierWave is an up and coming gem that is supposed to be good. I haven't tried CarrierWave, but will recommend paperclip--it just works. If you're going to be hosted on Heroku and dealing with files >5mb you'll need to use a direct to s3 uploader like this fork of s3-swf-upload because Heroku will kill any long processes like a paperclip upload.

I've worked with rubyzip for on the fly compression and had no complaints about it.

So what I'm trying to do is bulk upload a bunch of photos to the server and have them processed into S3 anyway, so I had already been starting to skeleton out some Paperclip-based models. This S3 uploader looks nice.

Trying to get my head wrapped around his example as far as using this with Paperclip though.

He's moving a file within the S3 bucket... Is the filename he's getting at line 39 here referring to the Paperclip derived path? And presumably he'd have declared a "has_attached_file :file" somewhere in the upload model, although it's not seen here? So if I did this, then I'd just have to move the upload out of wherever I staged it to, and then if I wanted thumbnails generated I'd just have to schedule a call to "reprocess!" in a background worker task?

hmm yes
Dec 2, 2000
College Slice

Pardot posted:

It's not so much of the process getting killed (I've had workers go for 16+ hours, though I really don't recommend that), direct to s3 is a better idea no matter where you're hosted because it won't tie up a web processes preventing requests while something is uploading.

I just assumed that it was the process getting killed because uploads using the paperclip defaults (upload to heroku -> process images -> upload to s3) will time out/fail on the first step if the file size gets too large.

kitten smoothie posted:

He's moving a file within the S3 bucket... Is the filename he's getting at line 39 here referring to the Paperclip derived path? And presumably he'd have declared a "has_attached_file :file" somewhere in the upload model, although it's not seen here? So if I did this, then I'd just have to move the upload out of wherever I staged it to, and then if I wanted thumbnails generated I'd just have to schedule a call to "reprocess!" in a background worker task?

Yes, that's right. I'm not too crazy about s3-swf-upload and have been trying to implement other solutions, but not much else seems to exist. The only way I know how to do a direct to s3 and tie into paperclip is using js postbacks like in that example. You'll see he uses a <%= notification_of_file_uploaded_path %> or similar path name--that needs to be the route that ties to the described s3_upload action.

Cock Democracy
Jan 1, 2003

Now that is the finest piece of chilean sea bass I have ever smelled
Am I the only one using apache/passenger to host rails apps on their own? After seeing the S3 failure, I'm surprised to still see so much support for EC2/heroku here.

NotShadowStar
Sep 20, 2000
It's a huge, huge pain in the rear end to deal with it yourself. Deployment can suck, you have to manage the security of the server, deal with scaling, contracting out data center space or some sort of hosting company, manage redundancy. The EC2 thing was a fluke, services were down but they came back up and everything is running.

So you can either micromanage a whole ton of poo poo yourself, hire someone to do it full time, or you can 'git push heroku master' and be done with it. If you need to scale you can log into the web interface and move some sliders around and now you've scaled more.

Obsurveyor
Jan 10, 2003

Cock Democracy posted:

Am I the only one using apache/passenger to host rails apps on their own? After seeing the S3 failure, I'm surprised to still see so much support for EC2/heroku here.
I host my own, the same way. Company wants everything kept on-site. You do not know how much I pine for getting our email hosted by Google but it will never happen. :(

edit: Oh yeah, it is definitely a pain in the rear end to keep everything in-sync and up-to-date, etc. Especially when you create a dev/staging/production setup and want to keep all the Apache configs in sync. Git Pusshuten was neat but built entirely around Ubuntu which is not my server OS of choice.

Obsurveyor fucked around with this message at 02:33 on Jun 22, 2011

8ender
Sep 24, 2003

clown is watching you sleep
I use a managed VPS through Speedyrails so I get the flexibility of an Apache/Passenger setup without all the hassle.

smug forum asshole
Jan 15, 2005
I'm starting to use OmniAuth. If I'm understanding correctly, it only connects with the Facebook API when a user needs to be authorized. Can I pull data from a user's facebook profile at will with OmniAuth?

(I'm working from Ryan Bates' set of OmniAuth Railscasts here.)

dustgun
Jun 20, 2004

And then the doorbell would ring and the next santa would come
Trying to get 3.1 running on dreamhost.
Shoot me.

Molten Llama
Sep 20, 2006
Dreamhost, pfft. You haven't truly lived in hell until you've tried to get any version running on Hostgator.

I've been putting off a job for my dad for three days because I know it will again culminate in an hour of trying to determine why it's causing a server error for no apparent reason.

smug forum asshole
Jan 15, 2005

Molten Llama posted:

Dreamhost, pfft. You haven't truly lived in hell until you've tried to get any version running on Hostgator.

I've been putting off a job for my dad for three days because I know it will again culminate in an hour of trying to determine why it's causing a server error for no apparent reason.

This concerns me, because I have a client who wants to deploy on Hostgator later this summer.

asveepay
Jul 7, 2005
internobody

smug forum rear end in a top hat posted:

I'm starting to use OmniAuth. If I'm understanding correctly, it only connects with the Facebook API when a user needs to be authorized. Can I pull data from a user's facebook profile at will with OmniAuth?

(I'm working from Ryan Bates' set of OmniAuth Railscasts here.)

In my experience, no you can't, but you do get a bunch of data back from facebook when they authenticate which you could save and process later.

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

smug forum rear end in a top hat posted:

I'm starting to use OmniAuth. If I'm understanding correctly, it only connects with the Facebook API when a user needs to be authorized. Can I pull data from a user's facebook profile at will with OmniAuth?

(I'm working from Ryan Bates' set of OmniAuth Railscasts here.)

You send a request to Facebook to access a user's account on his behalf. Facebook asks the user if that's cool. If the user says OK you get back a token and a secret to use later to access the user's info. OmniAuth handles negotiating with Facebook and gets the user token/secret for you. It's then on you to use those credentials (in conjunction with your application's key and secret) to actually access the data. There are lots of Facebook gems out there to handle the heavy lifting for you.

Bob Morales
Aug 18, 2006


Just wear the fucking mask, Bob

I don't care how many people I probably infected with COVID-19 while refusing to wear a mask, my comfort is far more important than the health and safety of everyone around me!

What are some ways of issuing a shell command from a rails app and showing the user the output?

For example, I'd have a list of network interfaces on the server, and you could click on button to turn the interface on/off, send a ping, or release/renew the DHCP address. These would just issue commands or run scripts I have written.

There's be some sort of indicator icon in the same row as the interface icon that told the status, but I'd like some sort of box that displays the output from that script or command for more information.

NotShadowStar
Sep 20, 2000
Kernel.system

Be very, very, very careful with this.

hepatizon
Oct 27, 2010

NotShadowStar posted:

Kernel.system

Be very, very, very careful with this.

Or `command`, which conveniently evaluates to the stdout of the command.

Adbot
ADBOT LOVES YOU

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.
You really, really, really don't want to execute shell commands from a script that accepts user-supplied data. I also doubt that you'd want to be able up/down a network interface or change its IP address via a web application.

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