Forum: Ruby How to find Operating system

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3083ca8f0496edaac2fa10c20630d0bc?d=identicon&s=25 Newb Newb (newb)
on 2009-04-08 12:08
Hi....
Here me looking for some help....
how to find web server's operating system ...

whether it is running under windows or linux...


Any helps

Thanks
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-04-08 12:50
(Received via mailing list)
On 08.04.2009 12:09, Newb Newb wrote:
> Here me looking for some help....
> how to find web server's operating system ...

Do you mean remotely?

> whether it is running under windows or linux...

It depends what the server in question is willing to provide as
information.  You can try to look at HTTP response header "Server" and
draw your conclusions (if it is IIS then the server must be some kind of
Windows).

Kind regards

  robert
146e52d49d361f85c0945487452fc6a0?d=identicon&s=25 Ben Lovell (benl)
on 2009-04-08 12:54
(Received via mailing list)
You can reach into the ENV object for a whole load of information about
your
current execution environment:

ENV.to_hash.each do |key, value|
    puts("#{key} - #{value}")
end
Fa2521c6539342333de9f42502657e5a?d=identicon&s=25 Eleanor McHugh (Guest)
on 2009-04-08 13:28
(Received via mailing list)
On 8 Apr 2009, at 11:53, Ben Lovell wrote:
> You can reach into the ENV object for a whole load of information
> about your
> current execution environment:
>
> ENV.to_hash.each do |key, value|
>    puts("#{key} - #{value}")
> end

Or you can use rbconfig:

   require 'rbconfig'
   case Config::CONFIG['host_os']
   when /darwin/i
     puts "Ah, Darwin :)"
   when /mswin|windows/i
     raise "You call that an operating system?"
   else
     raise "I haven't the foggiest"
   end

However be aware that if you're using JRuby it will still report the
underlying OS which might not be as informative as you'd like so
you'll also need to check other CONFIG options.


Ellie

Eleanor McHugh
Games With Brains
http://slides.games-with-brains.net
----
raise ArgumentError unless @reality.responds_to? :reason
570f9ad0ade7ac63b0a75cdb78939041?d=identicon&s=25 Mark S Bilk (Guest)
on 2009-04-08 14:30
(Received via mailing list)
On Apr 8, 4:27 am, Eleanor McHugh <elea...@games-with-brains.com>
wrote:
>    end
Thanks, Eleanor!  If I may suggest an additional clause
(with meaning in both the individual and group sense):

 require 'rbconfig'
   case Config::CONFIG['host_os']
   when /darwin/i
     puts "Ah, Darwin :)"
   when /linux-gnu/i
     puts "GNU/Linux - The World Community expands Consciousness"
   when /mswin|windows/i
     raise "You call that an operating system?"
   else
     raise "I haven't the foggiest"
   end
Fa2521c6539342333de9f42502657e5a?d=identicon&s=25 Eleanor McHugh (Guest)
on 2009-04-08 16:21
(Received via mailing list)
On 8 Apr 2009, at 13:30, Mark S Bilk wrote:
>     raise "You call that an operating system?"
>   else
>     raise "I haven't the foggiest"
>   end

*grumble* *grumble* GPL *grumble* ;p


Ellie

Eleanor McHugh
Games With Brains
http://slides.games-with-brains.net
----
raise ArgumentError unless @reality.responds_to? :reason
403b1743ad9522eeb22f301727ffc714?d=identicon&s=25 James French (Guest)
on 2009-04-08 16:30
(Received via mailing list)
Hi,

Could anyone suggest an elegant way of doing something like:

$directXSDKBootstrapped = false

def bootstrapDirectXSDK
  if !$directXSDKBootstrapped
    bootstrapBuildToolsDirectory("DirectX/#{DIRECTX_VERSION}")
    $directXSDKBootstrapped = true
    $directXSDKBootstrapped.freeze
  end
end

Obviously the intention is to only do something the first time it is
called. Maybe some little reusable util? I have a number of different
bootstrap functions I want to apply this to.

Cheers,
James
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-04-08 16:40
(Received via mailing list)
Don't think of them as "functions". Think of them as "methods", and
always put
them inside classes.

James French wrote:
>     $directXSDKBootstrapped.freeze
>   end
> end

class Whatever
  def bootstrapDirectXSDK
    @@directXSDKBootstrapped ||=
      bootstrapBuildToolsDirectory("DirectX/#{DIRECTX_VERSION}")
  end
end

That's teh "proxy pattern" in Ruby...
F1938de600a3feacd015051492fdaf0f?d=identicon&s=25 LAMBEAU Bernard (Guest)
on 2009-04-08 16:41
(Received via mailing list)
One way to do it, which requires a good understanding of
metaprogramming, could be as follows:

class Class

  # Replaces the method whose name is provided by a
  # method that calls the original one the first time it is called
  # and by a noop method for subsequent calls.
  def one_call_only(method_name)
    # implement me
  end

end

class MyClass
  def bootstrapDirectXSDK
    bootstrapBuildToolsDirectory("DirectX/#{DIRECTX_VERSION}")
  end
  one_call_only :bootstrapDirectXSDK
end

I'm not sure it's the simplest way to do it. I can sketch the actual
code of one_call_only if it helps.

blambeau

On Wed, Apr 8, 2009 at 4:29 PM, James French
53581739a445ad78250a676dabddf55f?d=identicon&s=25 James Coglan (Guest)
on 2009-04-08 16:43
(Received via mailing list)
> end
One way of doing this is as follows:

class Proc
  def runs(n)
    wrapped, count = self, 0
    lambda { |*args|
      count += 1
      count <= n ? wrapped.call(*args) : nil
    }
  end
end

>> foo = lambda { |arg| puts arg }.runs(3)
#=> #<Proc:0xb7d310dc@(irb):33>

>> foo['look!']
look!
=> nil
>> foo['look!']
look!
=> nil
>> foo['look!']
look!
=> nil
>> foo['look!']
=> nil

Notice nothing is printed the fourth time. So using this, you could
write
you function as:

bootstrapDirectXSDK = lambda {
  bootstrapBuildToolsDirectory("DirectX/#{DIRECTX_VERSION}")
}.runs(1)

Which will only allow its contents to be executed once.
4e39ce958e5ab660993b8b175b129803?d=identicon&s=25 Henrik Hodne (Guest)
on 2009-04-08 16:49
(Received via mailing list)
Look in the response headers, the server software should be there (and
often the OS)

Regards,
Henrik Hodne
403b1743ad9522eeb22f301727ffc714?d=identicon&s=25 James French (Guest)
on 2009-04-08 17:13
(Received via mailing list)
I see I still have some way to go with ruby :) Nice! Can't believe how
quick you knocked that up...
403b1743ad9522eeb22f301727ffc714?d=identicon&s=25 James French (Guest)
on 2009-04-08 17:37
(Received via mailing list)
I have to admit though, I don't understand the 'wrapped, count = self,
0' line...
87ef5d1e14b148eb596433bc17ffe690?d=identicon&s=25 Leo (Guest)
on 2009-04-08 17:43
(Received via mailing list)
> $directXSDKBootstrapped = false
>
> def bootstrapDirectXSDK
>   if !$directXSDKBootstrapped
>     bootstrapBuildToolsDirectory("DirectX/#{DIRECTX_VERSION}")
>     $directXSDKBootstrapped = true
>     $directXSDKBootstrapped.freeze
>   end
> end

You could redefine bootstrapDirectXSDK:

$directXSDKBootstrapped = false

def bootstrapDirectXSDK
    bootstrapBuildToolsDirectory("DirectX/#{DIRECTX_VERSION}")
    $directXSDKBootstrapped = true
    $directXSDKBootstrapped.freeze
    def bootstrapDirectXSDK
    end
end
53581739a445ad78250a676dabddf55f?d=identicon&s=25 James Coglan (Guest)
on 2009-04-08 17:46
(Received via mailing list)
2009/4/8 James French <James.French@naturalmotion.com>

> I have to admit though, I don't understand the 'wrapped, count = self, 0'
> line...


That's just parallel assignment, it's equivalent to:

wrapped = self
count = 0
403b1743ad9522eeb22f301727ffc714?d=identicon&s=25 James French (Guest)
on 2009-04-08 17:47
(Received via mailing list)
Ahh, thanks.
403b1743ad9522eeb22f301727ffc714?d=identicon&s=25 James French (Guest)
on 2009-04-08 17:49
(Received via mailing list)
I like it, but you get a warning with -w...
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-04-08 18:12
(Received via mailing list)
On 08.04.2009 16:29, James French wrote:

Please do not hijack other threads.

> Could anyone suggest an elegant way of doing something like:
>
> $directXSDKBootstrapped = false
>
> def bootstrapDirectXSDK
>   if !$directXSDKBootstrapped
>     bootstrapBuildToolsDirectory("DirectX/#{DIRECTX_VERSION}")
>     $directXSDKBootstrapped = true
>     $directXSDKBootstrapped.freeze

Freezing has no effect here because it does not prevent reassignment to
the variable.

>   end
> end
>
> Obviously the intention is to only do something the first time it is called Maybe some 
little reusable util?I have a number of different bootstrap functions I want to apply this 
to.

Why can't you just invoke the initialization once and be done?  If this
is in a file which is required then Ruby will take care of this
automatically.

Another solution:

class OnlyOnce
   def initialize(&b)
     raise "Need a block!" unless b
     @block = b
   end

   def get
     if @block
       @val = block.call
       @block = nil
     end

     @val
   end
end

$directXSDKBootstrapped = OnlyOnce.new do
   bootstrapBuildToolsDirectory("DirectX/#{DIRECTX_VERSION}")
end

Cheers

  robert
797ef431a5e1295b56c08e1db4c8d2df?d=identicon&s=25 botp (Guest)
on 2009-04-08 18:40
(Received via mailing list)
On Wed, Apr 8, 2009 at 10:29 PM, James French
<James.French@naturalmotion.com> wrote:
> Obviously the intention is to only do something the first time it is called. Maybe some 
little reusable util? I have a number of different bootstrap functions I want to apply 
this to.
>

sometimes, i apply ruby's nested methods feature...

eg,

botp@jedi-hopeful:~$ cat test.rb
def only_once_dumper
  def only_once
  end
end

def only_once
  puts "i ran once!"
  only_once_dumper
end

only_once
only_once
only_once

botp@jedi-hopeful:~$ ruby test.rb
i ran once!
botp@jedi-hopeful:~$

kind regards -botp
This topic is locked and can not be replied to.