Google Groups Home
Help | Sign in
Math::PI?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Daniel Schüle  
View profile  
 More options Dec 16 2005, 12:42 pm
Newsgroups: comp.lang.ruby
From: "Daniel Schüle" <u...@rz.uni-karlsruhe.de>
Date: Fri, 16 Dec 2005 18:42:17 +0100
Local: Fri, Dec 16 2005 12:42 pm
Subject: Math::PI?
Hello,

sorry for such a basic question
am I missing some module with math constants or are the users .. me
in this case :) supposed to define them on our own?
I know and do now
PI = Math.acos(-1)

thx

Daniel


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
ara.t.how...@noaa.gov  
View profile  
 More options Dec 16 2005, 12:46 pm
Newsgroups: comp.lang.ruby
From: ara.t.how...@noaa.gov
Date: Sat, 17 Dec 2005 02:46:50 +0900
Local: Fri, Dec 16 2005 12:46 pm
Subject: Re: Math::PI?

On Sat, 17 Dec 2005, Daniel [UNKNOWN] Schüle wrote:
> Hello,

> sorry for such a basic question
> am I missing some module with math constants or are the users .. me
> in this case :) supposed to define them on our own?
> I know and do now
> PI = Math.acos(-1)

> thx

??

harp:~ > irb
irb(main):001:0> Math::constants.grep /pi/i
=> ["PI"]
irb(main):002:0> Math::PI
=> 3.14159265358979

hth

-a
--
=========================================================================== ====
| ara [dot] t [dot] howard [at] noaa [dot] gov
| all happiness comes from the desire for others to be happy.  all misery
| comes from the desire for oneself to be happy.
| -- bodhicaryavatara
=========================================================================== ====


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniel Schüle  
View profile  
 More options Dec 16 2005, 3:05 pm
Newsgroups: comp.lang.ruby
From: Daniel Schüle <u...@rz.uni-karlsruhe.de>
Date: Fri, 16 Dec 2005 21:05:22 +0100
Local: Fri, Dec 16 2005 3:05 pm
Subject: Re: Math::PI?
[..]

> harp:~ > irb
> irb(main):001:0> Math::constants.grep /pi/i
> => ["PI"]
> irb(main):002:0> Math::PI
> => 3.14159265358979

> hth

yes, it helps
I am not sure whether I tried Math::PI or Math.PI
also I use Ruby on windows and linux
as I wrote my prevous post I used Ruby on windows

but it brings up an interessting point

module A
a=1
@b=2
@@c=3
$d=4
def e;5;end
def A.f;6;end
end

A::f is the same as A.f
$d is immediatly accessable
A::e, A.e accessable after include A

what is about a,b and c?
How can I put a constant in the module?

Regards, Daniel


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
ara.t.how...@noaa.gov  
View profile  
 More options Dec 16 2005, 3:17 pm
Newsgroups: comp.lang.ruby
From: ara.t.how...@noaa.gov
Date: Sat, 17 Dec 2005 05:17:49 +0900
Local: Fri, Dec 16 2005 3:17 pm
Subject: Re: Math::PI?

   module M
     A = 42
   end

   module N
     include M
     B = 42
   end

   class K
     include A
     include B
     C = 42
   end

   p M::A

   p N::A
   p N::B

   p K::A
   p K::B
   p K::C

cheers.

-a
--
=========================================================================== ====
| ara [dot] t [dot] howard [at] noaa [dot] gov
| all happiness comes from the desire for others to be happy.  all misery
| comes from the desire for oneself to be happy.
| -- bodhicaryavatara
=========================================================================== ====


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jacob Fugal  
View profile  
 More options Dec 16 2005, 5:50 pm
Newsgroups: comp.lang.ruby
From: Jacob Fugal <lukf...@gmail.com>
Date: Sat, 17 Dec 2005 07:50:31 +0900
Local: Fri, Dec 16 2005 5:50 pm
Subject: Re: Math::PI?
On 12/16/05, Daniel Schüle <u...@rz.uni-karlsruhe.de> wrote:

> module A
>   a=1
>   @b=2
>   @@c=3
>   $d=4
>   def e;5;end
>   def A.f;6;end
> end

> A::f is the same as A.f
> $d is immediatly accessable
> A::e, A.e accessable after include A

> what is about a,b and c?

$d is immediately available since the $ prefix makes it global.

'a' is a normal variable with local scope. The scope is the module
definition. So once you close the method definition the variable goes
away. It's still tied in the environment of any closures that
referenced it while it was "alive", however. This is useful for
instance in metaprogramming:

module Foo
  def bar
    "bar"
  end
  extend self
end

Foo.bar # => "bar"
# ... later ...

module Foo # reopening, not defining
  old_bar = method(:bar)
  define_method(:bar) do
    "foo" + old_bar.call
  end
end

old_bar # => raises NameError: undefined local variable or method `old_bar'
Foo.bar # => "foobar"

@b is an instance variable -- specifically, an instance variable on
the *current self* where it is used. In this case, that means it's an
instance variable on the Module object. So, if you reopen the module
and access @b, it will retain the value:

module Bar
  @test = 42
end

module Bar
  puts @test
end
# => 42

@@c is a class variable -- normally, a class variable on the class of
the current self. One would think that since the class of the Module
object defined is Module, all Modules will share class variables:

module A
  @@class_var = "xyzzy"
end

module B
  puts @@class_var
end

However, try the above and you'll get "uninitialized class variable
@@class_var in B". But if you reopen A, @@class_var is still there. I,
not knowing more, can only assume that ruby treats class variables
special and uses self as the container -- rather than self.class --
when self is a Class or Module.

What's the difference between @b and @@c at the class/module scope
then? As far as I can tell, none -- except confusion.

> How can I put a constant in the module?

Same as usual:

BAR = 42
BAR = 43 # => warning: already initialized constant BAR

module FOO
  BAR = 42
end

puts FOO::BAR # => 42

module
  BAR = 43
end
# => warning: already initialized constant BAR

Jacob Fugal


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google