Kernel#dir :- Returns the canonicalized absolute path of the
directory of the file from which this method is called. It means
symlinks in the path is resolved.
Can anyone tell me what the line means in **?
Thanks
Kernel#dir :- Returns the canonicalized absolute path of the
directory of the file from which this method is called. It means
symlinks in the path is resolved.
Can anyone tell me what the line means in **?
Thanks
What have you tried and what part of the output was confusing when you
tried it?
Adam P. wrote in post #1108982:
What have you tried and what part of the output was confusing when you
tried it?
what does it mean by canonicalized absolute path
?
Why said symlinks in the path is resolved
?
On 2013-May-14, at 14:31 , Love U Ruby wrote:
Adam P. wrote in post #1108982:
What have you tried and what part of the output was confusing when you
tried it?what does it mean by
canonicalized absolute path
?Why said
symlinks in the path is resolved
?
So that you don’t have multiple aliases for a single file (as the
require is path sensetive).
-Rob
Rob B. wrote in post #1108992:
what does it mean by
canonicalized absolute path
?Why said
symlinks in the path is resolved
?So that you don’t have multiple aliases for a single file (as the
require is path sensetive).-Rob
@Rob - Could you give me a simple example to understand this method,if
possible?
On 2013-May-15, at 10:27 , Love U Ruby wrote:
@Rob - Could you give me a simple example to understand this method,if
possible?
I hope you think this is simple enough. The Kernel#dir is essentially
File.dirname(File.realpath(FILE)).
[ruby-2.0.0p0] code/ruby $ mkdir symlink
[ruby-2.0.0p0] code/ruby $ cd symlink
[ruby-2.0.0p0] ruby/symlink $ ls -al
total 0
drwxr-xr-x 2 rab staff 68 May 15 11:00 .
drwxr-xr-x 171 rab staff 5814 May 15 11:00 …
[ruby-2.0.0p0] ruby/symlink $ mkdir -p real/path/to
[ruby-2.0.0p0] ruby/symlink $ touch real/path/to/file
[ruby-2.0.0p0] ruby/symlink $ ln -nsf real alt
[ruby-2.0.0p0] ruby/symlink $ ln -nsf real/path/to direct
[ruby-2.0.0p0] ruby/symlink $ ln -nsf alt/path/to indirect
[ruby-2.0.0p0] ruby/symlink $ ls -alR
total 24
drwxr-xr-x 6 rab staff 204 May 15 11:02 .
drwxr-xr-x 171 rab staff 5814 May 15 11:00 …
lrwxr-xr-x 1 rab staff 4 May 15 11:01 alt -> real
lrwxr-xr-x 1 rab staff 12 May 15 11:02 direct -> real/path/to
lrwxr-xr-x 1 rab staff 11 May 15 11:02 indirect -> alt/path/to
drwxr-xr-x 3 rab staff 102 May 15 11:00 real
./real:
total 0
drwxr-xr-x 3 rab staff 102 May 15 11:00 .
drwxr-xr-x 6 rab staff 204 May 15 11:02 …
drwxr-xr-x 3 rab staff 102 May 15 11:00 path
./real/path:
total 0
drwxr-xr-x 3 rab staff 102 May 15 11:00 .
drwxr-xr-x 3 rab staff 102 May 15 11:00 …
drwxr-xr-x 3 rab staff 102 May 15 11:01 to
./real/path/to:
total 0
drwxr-xr-x 3 rab staff 102 May 15 11:01 .
drwxr-xr-x 3 rab staff 102 May 15 11:00 …
-rw-r–r-- 1 rab staff 0 May 15 11:01 file
[ruby-2.0.0p0] ruby/symlink $ echo “Hi!” >> real/path/to/file
-bash: !": event not found
[ruby-2.0.0p0] ruby/symlink $ echo “Hello” >> real/path/to/file
[ruby-2.0.0p0] ruby/symlink $ cat alt/path/to/file
Hello
[ruby-2.0.0p0] ruby/symlink $ cat direct/file
Hello
[ruby-2.0.0p0] ruby/symlink $ cat indirect/file
Hello
[ruby-2.0.0p0] ruby/symlink $ echo “, World” >> real/path/to/file
[ruby-2.0.0p0] ruby/symlink $ cat alt/path/to/file
Hello
, World
[ruby-2.0.0p0] ruby/symlink $ cat direct/file
Hello
, World
[ruby-2.0.0p0] ruby/symlink $ cat indirect/file
Hello
, World
[ruby-2.0.0p0] ruby/symlink $ irb
irb2.0.0> File.realpath(‘indirect/file’)
#2.0.0 => “/Users/rab/code/ruby/symlink/real/path/to/file”
irb2.0.0> File.realpath(‘direct/file’)
#2.0.0 => “/Users/rab/code/ruby/symlink/real/path/to/file”
irb2.0.0> File.realpath(‘alt/path/to/file’)
#2.0.0 => “/Users/rab/code/ruby/symlink/real/path/to/file”
irb2.0.0> File.dirname(File.realpath(‘alt/path/to/file’))
#2.0.0 => “/Users/rab/code/ruby/symlink/real/path/to”
-Rob
On May 18, 2013 8:33 PM, “Love U Ruby” [email protected] wrote:
Thanks
How about just run it in irb? If you don’t know what a call stack is,
this
isn’t the method for you.
Hint: define a function that calls it. Then define a function that calls
the first function.
From the doc:
caller_locations(start=1, length=nil) → array or nil click to toggle
source
caller_locations(range) → array or nil
*Returns the current execution stack—an array containing backtrace
location objects.
*The optional start parameter determines the number of initial stack
entries to omit from the top of the stack.
*A second optional length parameter can be used to limit how many
entries are returned from the stack.
*Returns nil if start is greater than the size of current execution
stack.
Could anyone give me small snippets to understand this methods?
Thanks
Matthew K. wrote in post #1109455:
On May 18, 2013 8:33 PM, “Love U Ruby” [email protected] wrote:
Hint: define a function that calls it. Then define a function that calls
the first function.
Thanks for your hints,i tried and understood:
def test
caller_locations(1,2)
end
def chk
test
end
test #=> [“/home/kirti/ruby/test.rb:7:in `'”]
chk #=> [“/home/kirti/ruby/test.rb:5:in `chk’”,
“/home/kirti/ruby/test.rb:8:in `'”]
def test
caller_locations(1,1)
end
def chk
test
end
p chk #=> [“/home/kirti/ruby/test.rb:14:in `chk’”]
The only thing I do not understand is - *The optional start parameter
determines the number of initial stack entries to omit from the top of
the stack. - I used in my example optional start parameter
as 1
.
But not seen any effect of its. I am sure i don’t understand about the
usefulness of that parameter. please help.
Yes, Now I understood this Kernel# caller_locations method.
def test
caller_locations
end
def chk
test
end
p chk #=> ["/home/kirti/ruby/test.rb:5:in chk'", "/home/kirti/ruby/test.rb:7:in
‘"]
def test
caller_locations(1)
end
def chk
test
end
p chk #=> ["/home/kirti/ruby/test.rb:5:in chk'", "/home/kirti/ruby/test.rb:7:in
‘"]
def test
caller_locations(0)
end
def chk
test
end
p chk #=> ["/home/kirti/ruby/test.rb:16:in test'", "/home/kirti/ruby/test.rb:19:in
chk’", "/home/kirti/ruby/test.rb:21:in
`’"]
def test
caller_locations(0…2)
end
def chk
test
end
p chk #=> ["/home/kirti/ruby/test.rb:16:in test'", "/home/kirti/ruby/test.rb:19:in
chk’", “/home/kirti/ruby/test.rb:21:in
`’”]
@Matthew thanks for your hints.
Is there any difference between kernel#caller
and
kernel#caller_locations
? Looking at the below code I am not able to
find any difference between them?
def test
caller(0…2)
end
def chk
test
end
p chk
#=> ["/home/kirti/ruby/test.rb:3:in test'", "/home/kirti/ruby/test.rb:6:in
chk’", “/home/kirti/ruby/test.rb:8:in
`’”]
def test
caller_locations(0…2)
end
def chk
test
end
p chk
#=> ["/home/kirti/ruby/test.rb:3:in test'", "/home/kirti/ruby/test.rb:6:in
chk’", “/home/kirti/ruby/test.rb:8:in
`’”]
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs