What does the question mark at the end of a method name mean in Ruby?
It is a code style convention; it indicates that a method returns a boolean value (true or false) or an object to indicate a true value (or “truthy” value).
The question mark is a valid character at the end of a method name.
https://docs.ruby-lang.org/en/2.0.0/syntax/methods_rdoc.html#label-Method+Names
Get a the name of the variable that was sent as a parameter
No, it's not possible. Variable names only exist in the scope of the method that declare them.
What is passed to the method is not the variable itself, but the value of the variable, so you can't access the name. And even in the case of ref
parameters, what is passed is the memory location of the variable, without its name.
Why can't we use of hyphen while declaring structure variable name?
The boring answer is that the language definition doesn't allow -
to be part of an identifier (variable name, function name, typedef name, enumeration constant, tag name, etc.).
Why that's the case probably boils down to a couple of things:
At the preprocessing stage, your source text is broken up into a sequence of tokens - identifiers, punctuators, string literals, and numeric constants. Whitespace is not significant except that it separates tokens of the same type. If you write a=b+c;
, the compiler sees the sequence of tokens identifier (a
), punctuator (=
), identifier (b
), punctuator (+
), identifier (c
), and punctuator (;
). This is before it does any syntax analysis - it's not looking at the meaning or the structure of that statement, it's just breaking it down into its component parts.
It can do this because the characters =
and +
and ;
can never be part of an identifier, so it can clearly see where identifiers begin and end1.
The tokenizer is "greedy" and will build the longest valid token it can. In a declaration like
int a;
you need the whitespace to tell the preprocessor that int
and a
are separate tokens, otherwise it will try to mash them together into a single token inta
. Similarly, in a statement like a=b- -c;
, you need that whitespace (or parentheses, a=b-(-c);
) to signify you're subtracting -c
from b
, otherwise the tokenizer will interpret it as a = b-- c
, which isn't what you want.
So, if a -
could be part of an identifier, how should x=a-b+c
be tokenized? Is a-b
a single token or three? How would you write your tokenizer such that it could keep track of that? Would you require whitespace before and after -
to signify that it's an operator and not part of a variable?
It's certainly possible to define a language that allows -
to be both an operator and part of an identifier (see COBOL), but it adds complexity to the tokenizing stage of compiling, and it's just plain easier to not allow it.
- Coincidentally, this is why there's no difference between
T *p;
andT* p;
when declaring pointer variables - the*
can never be part of an identifier, so whitespace isn't necessary to separate the type from the variable name. You could write it asT*p;
or evenT * p;
and it will be treated exactly the same.
Why can't variable names start with numbers?
Because then a string of digits would be a valid identifier as well as a valid number.
int 17 = 497;
int 42 = 6 * 9;
String 1111 = "Totally text";
In Java, the variable name can be same with the classname
The compiler can tell by context. In the example you have given:
ClassName ClassName = new ClassName();
1 2 3
It can see that 1 is where a type name should be, so it knows you mean the class. Then, 2 is where a variable name is expected, so it knows that this should be the name of a variable. And 3 is coming after the new
keyword with parentheses, so it must be the name of a class.
System.out.println( ClassName );
In this instance, ClassName
is in the context of argument passing. A type name can't be passed as an argument, so you must mean the name of the variable.
To amuse yourself, you can change the print statement to:
System.out.println( ClassName.class );
Hover your mouse cursor on ClassName
and you'll see that the compiler recognizes this as the name of a class. Then change it to:
System.out.println( ClassName.getClass() );
Hover your cursor again, and now you see that it recognizes it as the variable name. That's because .class
can only be applied to a type name, while getClass()
can only be applied to an object reference. The result of the print statement would be the same in both cases - but through different mechanisms.
So the compiler has no problem here. But you are right that it's not readable to humans. The convention is that names of variables and methods must start with a lowercase letter, while type names must start with an uppercase letter. Adhering to this convention will ensure that no such readability problems arise.
I can't say exactly why the authors of Java chose not to enforce this convention (that is, give a compiler error if type names started with a lowercase letter or variable/method names started with an uppercase), but I speculate that they didn't want to make anything an actual error unless it would actually cause an ambiguity for the compiler. Compilation errors are supposed to indicate a problem that makes the compiler unable to do its work.
Method and variable name is the same
Try this:
puts hello()
Am I immoral for using a variable name that differs from its type only by case?
What is the reasoning of those telling you this is bad? I do this all the time. It is the simplest, expressive way to name a single variable of a type. If you needed two Person
objects then you could prefix person
with meaningful adjectives like
fastPerson
slowPerson
otherwise just
person
is fine with me.
Why do we use _ in variable names?
It doesn't mean anything. It is rather a common naming convention for private member variables to keep them separated from methods and public properties. For example:
class Foo
{
private int _counter;
public int GetCounter()
{
return _counter;
}
public int SetCounter(int counter)
{
_counter = counter;
}
}
Related Topics
How to Validate Exits and Aborts in Rspec
How to Generate Zip File Without Saving to the Disk with Ruby
Differencebetween Link_To, Redirect_To, and Render
How to Dynamically Create a Local Variable in Ruby
Portable Ruby on Rails Environment
Tell Ruby Program to Wait Some Amount of Time
"Which in Ruby": Checking If Program Exists in $Path from Ruby
Open the Default Browser in Ruby
Install Rails 3 on Osx with Rvm
How to Make Instance Variables Private in Ruby
How to Decompress Gzip String in Ruby
How to Prevent Nokogiri from Adding <Doctype> Tags