[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Help with customising the IRB prompt

Peter B.

2/17/2007 7:21:00 PM

Hi, I'm new to this newsgroup, so I'm not sure if this is the right
place to ask.

Ok, so what I'm trying to do is customise the IRB prompt in a rather
complicated way: I want to print %i*2 (or even just %i) spaces at the
beginning of each line of prompt. Here was my best attempt (not sure
if there's a better way to format this for Usenet):

IRB.conf[:PROMPT][:INDENTS] = {
:PROMPT_I => "#{%i.times {print ' '}} >> ",
:PROMPT_S => "#{%i.times {print ' '}} >> ",
:PROMPT_C => "#{%i.times {print ' '}} >> ",
:RETURN => "=> %s\n"
}

So, what I want it do is like this:

>> if "Jim" == "Fred"
>> print "Ruby Sucks!"
>> else
>> print "Ruby is cool!"
>> end
>> print "And you know it!"

But obviously, It tries to work out the value of the #{ } as soon as
it's set, not when it's displayed, after the %i has been gsub'd for
displaying. So, I get an error thrown at me.

Does anybody know how to do this? Maybe some printf() trickery?

Thanks in advance.

3 Answers

SonOfLilit

2/17/2007 7:37:00 PM

0

This is a constant so you cannot just assign it a dynamic value.

If you really want, you can assign it an instance of a class that
exposes an interface similar to that of a Hash but acts dynamically.

You'd still need to somehow get the value of "%i", whatever that is
(I'm not familiar with IRB configuration but I know it's very hard to
achieve this with Ruby so I just guess you meant @i or something (if
I'm wrong, sorry)).

You might as well just modify a few lines of IRB code, I guess.

By the way, printf() like formatitng in Ruby has a great syntax:

"%f" % 0.1
"%d %d" % [1, 2]

(String could be dynamic, Numerics could be dynamic, Array could be dynamic).


Have a look at the Adopt-a-newbie thread here in the mailing list :-)


Aur Saraf

On 2/17/07, Peter B. <peter.bunyan@googlemail.com> wrote:
> Hi, I'm new to this newsgroup, so I'm not sure if this is the right
> place to ask.
>
> Ok, so what I'm trying to do is customise the IRB prompt in a rather
> complicated way: I want to print %i*2 (or even just %i) spaces at the
> beginning of each line of prompt. Here was my best attempt (not sure
> if there's a better way to format this for Usenet):
>
> IRB.conf[:PROMPT][:INDENTS] = {
> :PROMPT_I => "#{%i.times {print ' '}} >> ",
> :PROMPT_S => "#{%i.times {print ' '}} >> ",
> :PROMPT_C => "#{%i.times {print ' '}} >> ",
> :RETURN => "=> %s\n"
> }
>
> So, what I want it do is like this:
>
> >> if "Jim" == "Fred"
> >> print "Ruby Sucks!"
> >> else
> >> print "Ruby is cool!"
> >> end
> >> print "And you know it!"
>
> But obviously, It tries to work out the value of the #{ } as soon as
> it's set, not when it's displayed, after the %i has been gsub'd for
> displaying. So, I get an error thrown at me.
>
> Does anybody know how to do this? Maybe some printf() trickery?
>
> Thanks in advance.
>
>
>

SonOfLilit

2/17/2007 7:42:00 PM

0

I was a bit unclear, so let me give an example:

class Hashlike < Hash
def [] (key)
return " " * $indentation_level if key == :PROMPT_I
super
end
end


IRB.conf[:PROMPT][:INDENTS] = Hashlike.new( # can I pass a Hash to Hash.new ?
:PROMPT_I => "#{%i.times {print ' '}} >> ",
:PROMPT_S => "#{%i.times {print ' '}} >> ",
:PROMPT_C => "#{%i.times {print ' '}} >> ",
:RETURN => "=> %s\n"

You might be able to achieve a similar effect with a Hash and a
default block, I don't remember it's syntax.


Aur Saraf


On 2/17/07, SonOfLilit <sonoflilit@gmail.com> wrote:
> This is a constant so you cannot just assign it a dynamic value.
>
> If you really want, you can assign it an instance of a class that
> exposes an interface similar to that of a Hash but acts dynamically.
>
> You'd still need to somehow get the value of "%i", whatever that is
> (I'm not familiar with IRB configuration but I know it's very hard to
> achieve this with Ruby so I just guess you meant @i or something (if
> I'm wrong, sorry)).
>
> You might as well just modify a few lines of IRB code, I guess.
>
> By the way, printf() like formatitng in Ruby has a great syntax:
>
> "%f" % 0.1
> "%d %d" % [1, 2]
>
> (String could be dynamic, Numerics could be dynamic, Array could be dynamic).
>
>
> Have a look at the Adopt-a-newbie thread here in the mailing list :-)
>
>
> Aur Saraf
>
> On 2/17/07, Peter B. <peter.bunyan@googlemail.com> wrote:
> > Hi, I'm new to this newsgroup, so I'm not sure if this is the right
> > place to ask.
> >
> > Ok, so what I'm trying to do is customise the IRB prompt in a rather
> > complicated way: I want to print %i*2 (or even just %i) spaces at the
> > beginning of each line of prompt. Here was my best attempt (not sure
> > if there's a better way to format this for Usenet):
> >
> > IRB.conf[:PROMPT][:INDENTS] = {
> > :PROMPT_I => "#{%i.times {print ' '}} >> ",
> > :PROMPT_S => "#{%i.times {print ' '}} >> ",
> > :PROMPT_C => "#{%i.times {print ' '}} >> ",
> > :RETURN => "=> %s\n"
> > }
> >
> > So, what I want it do is like this:
> >
> > >> if "Jim" == "Fred"
> > >> print "Ruby Sucks!"
> > >> else
> > >> print "Ruby is cool!"
> > >> end
> > >> print "And you know it!"
> >
> > But obviously, It tries to work out the value of the #{ } as soon as
> > it's set, not when it's displayed, after the %i has been gsub'd for
> > displaying. So, I get an error thrown at me.
> >
> > Does anybody know how to do this? Maybe some printf() trickery?
> >
> > Thanks in advance.
> >
> >
> >
>

Giles Bowkett

2/18/2007 3:43:00 PM

0

IRB.conf[:AUTO_INDENT]=true

It's not actually what you want, but it's pretty good.

--
Giles Bowkett
http://www.gilesg...
http://gilesbowkett.bl...
http://gilesgoatboy.bl...


On 2/17/07, SonOfLilit <sonoflilit@gmail.com> wrote:
> I was a bit unclear, so let me give an example:
>
> class Hashlike < Hash
> def [] (key)
> return " " * $indentation_level if key == :PROMPT_I
> super
> end
> end
>
>
> IRB.conf[:PROMPT][:INDENTS] = Hashlike.new( # can I pass a Hash to Hash.new ?
> :PROMPT_I => "#{%i.times {print ' '}} >> ",
> :PROMPT_S => "#{%i.times {print ' '}} >> ",
> :PROMPT_C => "#{%i.times {print ' '}} >> ",
> :RETURN => "=> %s\n"
>
> You might be able to achieve a similar effect with a Hash and a
> default block, I don't remember it's syntax.
>
>
> Aur Saraf
>
>
> On 2/17/07, SonOfLilit <sonoflilit@gmail.com> wrote:
> > This is a constant so you cannot just assign it a dynamic value.
> >
> > If you really want, you can assign it an instance of a class that
> > exposes an interface similar to that of a Hash but acts dynamically.
> >
> > You'd still need to somehow get the value of "%i", whatever that is
> > (I'm not familiar with IRB configuration but I know it's very hard to
> > achieve this with Ruby so I just guess you meant @i or something (if
> > I'm wrong, sorry)).
> >
> > You might as well just modify a few lines of IRB code, I guess.
> >
> > By the way, printf() like formatitng in Ruby has a great syntax:
> >
> > "%f" % 0.1
> > "%d %d" % [1, 2]
> >
> > (String could be dynamic, Numerics could be dynamic, Array could be dynamic).
> >
> >
> > Have a look at the Adopt-a-newbie thread here in the mailing list :-)
> >
> >
> > Aur Saraf
> >
> > On 2/17/07, Peter B. <peter.bunyan@googlemail.com> wrote:
> > > Hi, I'm new to this newsgroup, so I'm not sure if this is the right
> > > place to ask.
> > >
> > > Ok, so what I'm trying to do is customise the IRB prompt in a rather
> > > complicated way: I want to print %i*2 (or even just %i) spaces at the
> > > beginning of each line of prompt. Here was my best attempt (not sure
> > > if there's a better way to format this for Usenet):
> > >
> > > IRB.conf[:PROMPT][:INDENTS] = {
> > > :PROMPT_I => "#{%i.times {print ' '}} >> ",
> > > :PROMPT_S => "#{%i.times {print ' '}} >> ",
> > > :PROMPT_C => "#{%i.times {print ' '}} >> ",
> > > :RETURN => "=> %s\n"
> > > }
> > >
> > > So, what I want it do is like this:
> > >
> > > >> if "Jim" == "Fred"
> > > >> print "Ruby Sucks!"
> > > >> else
> > > >> print "Ruby is cool!"
> > > >> end
> > > >> print "And you know it!"
> > >
> > > But obviously, It tries to work out the value of the #{ } as soon as
> > > it's set, not when it's displayed, after the %i has been gsub'd for
> > > displaying. So, I get an error thrown at me.
> > >
> > > Does anybody know how to do this? Maybe some printf() trickery?
> > >
> > > Thanks in advance.
> > >
> > >
> > >
> >
>
>