SO...
Sublime Text has the built in ability to fold methods, but as soon as the method declaration spans multiple lines it loses this ability. Does anyone know about a plugin or a way to make this work? Specifically I am running into this with ruby (and my team adheres to a strict style guide regarding line length), but the language shouldn't matter.
Instead of clicking on the down arrow that appears in the gutter next to the first line of the function definition, all you need to do is put your cursor on one of the function's indented lines (not the indented function parameters, but in the function definition itself) and use the CtrlShift[ key binding (on OS X use ⌘Alt[) to fold the function and its parameters. Use CtrlShift] (⌘Alt] on OS X) to unfold, or you can click the sideways arrow next to the def line.
So, this:
becomes this:
Try this formatting
def example( # <--- folds parameters here
param,
param
) # <--- folds method body here
foo
bar
qux
end
The language does matter as far as I know, method folding is part of the language specific configuration. At least it used to be in previous versions.
Related
I can't understand well about the last ways of arrow functions have:
No duplicate named arguments- arrow functions cannot have duplicate
named arguments in strict or nonstrict mode, as opposed to nonarrow
functions that cannot have duplicate named arguments only in strict
mode.
The above paragraph was picked from Book "Understanding ECMAScript 6" wrote by Nicholas C. Zakas in 'Function' chapter.
According to description above, I know that arrow function has not arguments like other function.
I can understand well the sentence before half, but the other half start by "as opposed to...".
What's that mean "nonarrow functions that cannot have duplicate named arguments only in strict mode."
In fact, functions in strict mode also have arguments. I have no idea what the author mean.
It means that the following is valid JavaScript:
function bar(foo, foo){}
It is not, however, when using strict mode:
'use strict';
function bar(foo, foo){}
// SyntaxError: duplicate formal argument foo
With arrow functions, duplicate named arguments are always, regardless of strict or non-strict mode, invalid.
(foo, foo) => {}
// SyntaxError: duplicate argument names not allowed in this context
According to description above, I know that arrow function has not arguments like other function.
Not sure whether you understood this correctly. Arrow functions can have parameters, it just does not have arguments.
In java if you write myInstance.(list of methods here) you get a list of all methods, but in objective c you write [myInstance (method)] and you have to write a character to see all methods who starts with this character.
It is possible to show all methods without writing the first character? like [myInstance (list of methods here)]
Thanks!
Yes, it is possible, but you have to write in this way
[myInstance *]
instead of *, you have to press Esc key....
Xcode does have intellisense. Just hit the ESC key after a space and it will display all the methods.
What is the preferred, accepted, best practice, etc. for parentheses in CoffeeScript method/function calls?
foo(bar, baz).zap?
(foo bar, baz).zap?
Always 1 or always 2? Or something else / it depends (please elaborate)
The first style is more common. One reason is that in chains, only the first style can be used at every step of the chain:
foo(bar, baz).zap(yota).penumbra
However, consider that
new Foo.bar()
means "create a new instance of Foo.bar, whereas
(new Foo).bar()
means "create a new instance of Foo and call the bar method on that instance." By analogy to new, I prefer to use the second style when using a function like Node's require:
(require 'crypto').createHash 'sha1'
I am new to Ruby and am learning from reading an already written code.
I encounter this code:
label = TkLabel.new(#root) do
text 'Current Score: '
background 'lightblue'
end
What is the semantics of the syntax "do" above?
I played around with it and it seems like creating a TkLabel object then set its class variable text and background to be what specified in quote. However when I tried to do the same thing to a class I created, that didn't work.
Oh yeah, also about passing hash into function, such as
object.function('argument1'=>123, 'argument2'=>321)
How do I make a function that accepts that kind of argument?
Thanks in advance
What you're looking at is commonly referred to as a DSL, or Domain Specific Language.
At first glance it may not be clear why the code you see works, as text and background are seemingly undefined, but the trick here is that that code is actually evaluated in a scope in which they are. At it's simplest, the code driving it might look something like this:
class TkLabel
def initialize(root, &block)
#root = root
if block
# the code inside the block in your app is actually
# evaluated in the scope of the new instance of TkLabel
instance_eval(&block)
end
end
def text(value)
# set the text
end
def background(value)
# set the background
end
end
Second question first: that's just a hash. Create a function that accepts a single argument, and treat it like a hash.
The "semantics" are that initialize accepts a block (the do...end bit), and some methods accepting string parameters to set specific attributes.
Without knowing how you tried to do it, it's difficult to go much beyond that. Here are a few, possible, references that might help you over some initial hurdles.
Ruby is pretty decent at making miniature, internal DSLs because of its ability to accepts blocks and its forgiving (if arcane at times) syntax.
Excuse my emacs newbiness here, but does anybody know how to get around this? When coding in emacs, in ruby-mode, it indents to the correct level (i.e. by 2 spaces) after all the keywords, like def, class, module, begin etc, but when breaking parameter lists across multiple lines, it indents to a seemingly random position, like 40 or so columns over.
I've been reading around emacs tab settings and seem to just be going around in circles and not getting to information I'm looking for, so I figured I'd ask here.
Here's a screenshot of where it is placing the cursor in a parameter list. I've tried indenting inside of curly braces (e.g. for a block, or a hash) and that is working ok, it's the parentheses that are messing it up.
http://compgroups.net/comp.emacs/Ruby-mode-indentation-of-continuation-lines
(setq ruby-deep-indent-paren nil)
Or temporarily, within the current session:
M-x set-variable RET ruby-deep-indent-paren RET nil RET
Inside of a parentheses it will now indent like it does everywhere else. There is still a minor bug in the case of what I posted above. It indents 2 spaces further than I want it to, because I'm confusing it with the combination of ( and {.
ruby-deep-indent-paren and related vars have no effect for me because ruby-use-smie is t. Setting both to nil didn't seem to help either :-(
But switching to enh-ruby-mode, it's working!
Setting enh-ruby-deep-indent-paren to nil had an effect.
Setting enh-ruby-bounce-deep-indent to t allows me to press Tab again to toggle between the styles!
Basically it's trying to line up the args in a multi-line list of parenthesized arguments, like:
function_call (arg1,
arg2);
Setting the ruby-deep-indent-paren to nil as above changes the behvaior to the annoying double-indenting for mixed braces, e.g.:
if (cond) then
do_stuff
end
function_call (&proc {
do_stuff
})
The indenting wierdness is really bothering me. I edited Mats' original ruby-mode.el code to try and indent more sanely. But I can't get it cleaned up for the life of me.