In macruby what does Module's method "__type__" do? - macruby

Module in macruby has many methods that it doesn't usually have. One of these is __type__ and I simply can't seem to figure out what it does. What does it do?
Thanks!
z.

__type__ is defined in object.c as:
static VALUE
rb_obj_type(VALUE obj)
{
return LONG2FIX(TYPE(obj));
}
which in turns depends on the rb_type function:
static inline int
rb_type(VALUE obj)
{
if (IMMEDIATE_P(obj)) {
if (FIXNUM_P(obj)) {
return T_FIXNUM;
}
if (FIXFLOAT_P(obj)) {
return T_FLOAT;
}
if (obj == Qtrue) {
return T_TRUE;
}
if (obj == Qundef) {
return T_UNDEF;
}
}
else if (!RTEST(obj)) {
if (obj == Qnil) {
return T_NIL;
}
if (obj == Qfalse) {
return T_FALSE;
}
}
return rb_objc_type(obj);
}
Definitely, it simply returns a number that corresponds to a type identifier, as defined by the pre compiler constants T_FIXNUM, T_FLOAT, etc.
I would say that it is something of really limited use for standard users, although it may make your code more efficient in type checking when you write C extensions.

Related

What is this macro?

In ruby.h, there are a lot of function macros defined like this:
static inline int
#if defined(HAVE_PROTOTYPES)
rb_type(VALUE obj)
#else
rb_type(obj)
VALUE obj;
#endif
{
if (FIXNUM_P(obj)) return T_FIXNUM;
if (obj == Qnil) return T_NIL;
if (obj == Qfalse) return T_FALSE;
if (obj == Qtrue) return T_TRUE;
if (obj == Qundef) return T_UNDEF;
if (SYMBOL_P(obj)) return T_SYMBOL;
return BUILTIN_TYPE(obj);
}
If HAVE_PROTOTYPES==1, from my understanding, this function will be like this:
static inline int rb_type(VALUE obj)
{
...
}
Yet, if HAVE_PROPOTYPES==0, the function definition will be like this:
static inline int rb_type(VALUE obj)
VALUE obj;
{
...
}
I don't understand if this is grammatically correct. How should I understand it?
static inline int rb_type(VALUE obj)
VALUE obj; # what the hack is this?
{
...
}
This is K&R C. Nobody uses it anymore. It has been deprecated for 20 years at least.
Long time ago function definitions were written like this:
int myfunc(myparam)
int myparam;
{
...
}
instead of
int myfunc(int myparam)
{
...
}
So HAVE_PROTOTYPES will always be defined on any decent compiler.

store data in array from another class method

i have one method in class call Class1 like
' public void getEventFromUser() {
int event;
Scanner input = new Scanner(System.in);
date.getDateFromUser();
// od.inputday();
// od.inputyear();
time.getTimeFromUser();
System.out.println("Enter description :");
description = input.nextLine();
}'
and i want to execute this method and store in array in another class
like
public void addEvent() {
if (numEvent == maxEvent) {
System.out.println("error…no more room to add events");
} else {
schedule[numEvent]=getEventFromUser();
int count = 0;
while (count < numEvent - 1) {
if (schedule[numEvent].isEqual(schedule[count])) {
isFound = true;
break;
}
count++;
if (isFound == true) {
System.out.println("Event already exists-notadding");
schedule[numEvent] = null;
} else {
schedule[numEvent].setDate();
schedule[numEvent].setTime();
schedule[numEvent].setDescription();
numEvent++;
//schedule[numEvent]=schedule[numEvent].getEventFromUser();
}
}
}
} '
so,how can i do this?
pls give me some solution
getEventFromUser() doesn't return a value, which is why schedule[numEvent]=schedule[numEvent].getEventFromUser() is giving you trouble.
Without knowing a bit more about what you're trying to do, it's hard to say if you should have getEventFromUser() return a value or have getEventFromUser() directly store a value in a field in the class. (I'm guessing the setDate, setTime and setDescription methods do this.)

core-plot Mixing CPTScatterPlotInterpolationCurved and CPTScatterPlotInterpolationLinear

I need to be able to draw sequential line segments that have the same Y Coordinate with CPTScatterPlotInterpolationLinear and ones that do not with CPTScatterPlotInterpolationCurved.
As CPTScatterPlotInterpolationCurved draws all lines curved. I am currently doing this by adding multiple plots.
public List<CorrectedGraphPoints> GetCorrectDataPoints(List<PointF> dataSource)
{
int lastIndex = 0;
bool shouldLoop = true;
CPTScatterPlotInterpolation interpolation = CPTScatterPlotInterpolation.Curved;
List<CorrectedGraphPoints> OuterList = new List<CorrectedGraphPoints> ();
if (dataSource [0].Y == dataSource [1].Y)
interpolation = CPTScatterPlotInterpolation.Linear;
while (lastIndex+1 != dataSource.Count) {
OuterList.Add (new CorrectedGraphPoints (interpolation));
while (shouldLoop)
{
OuterList[OuterList.Count -1].Add(dataSource[lastIndex]);
if ((lastIndex + 1) < dataSource.Count) {
if (interpolation == CPTScatterPlotInterpolation.Linear) {
if (dataSource [lastIndex].Y != dataSource [lastIndex + 1].Y) {
interpolation = CPTScatterPlotInterpolation.Curved;
shouldLoop = false;
break;
}
}
if (interpolation == CPTScatterPlotInterpolation.Curved) {
if (dataSource [lastIndex].Y == dataSource [lastIndex + 1].Y) {
interpolation = CPTScatterPlotInterpolation.Linear;
shouldLoop = false;
break;
}
}
}
else {
shouldLoop = false;
break;
}
if (shouldLoop)
lastIndex++;
}
shouldLoop = true;
}
return OuterList;
}
public class CorrectedGraphPoints
{
private List<PointF> points;
public List<PointF> Points { get { return points; } }
private CPTScatterPlotInterpolation interpolation;
public CPTScatterPlotInterpolation Interpolation { get { return interpolation; } }
public CorrectedGraphPoints(CPTScatterPlotInterpolation interpolation)
{
this.interpolation = interpolation;
points = new List<PointF> ();
}
public void Add(PointF point)
{
points.Add (point);
}
}
However creating multiple plots that use fill slows the app down tremendously. I was wondering if I could limit how much I do this? I haven't been able to find a way to change the interpolation for a section?? IS this an just an issue with core plot or is it something wrong with my logic or code?
Another possible solution would be to add additional points to your data to draw the curved sections. This would allow you to use only one plot. The number of additional points needed will depend on several factors including the size of the plot and the line style used to draw the line.

Visual Studio 2012 debugger fails computing index of managed C++/CLI array

I have found what seems to be an anomaly in VS2012 debugger display of managed arrays using C++/CLI. It seems when I try to use a simple math expression for the index the debugger displays element 0 instead. See watch window below.
The declaration of the array looks like this.
/* Stack where the values of tokens are stored */
array<YYSTYPE>^ yyv = gcnew array<YYSTYPE> (YYMAXDEPTH);
If elements are expanded it shows for certain that the yyv[4-1] element has the same address as the yyv[0] element.
Is it not possible to use expressions for default indexer of managed objects in Visual Studio debugger with C++/CLI?
YYSTYPE is declared as a managed value struct containing a single Object^ reference as follows:
value struct YYSTYPE
{
Object^ obj;
/* Constant integer value */
property long int4
{
long get() { return safe_cast<long>(obj); }
void set(long value) { obj = safe_cast<Object^>(value); }
}
/* Constant floating point value */
property float fp
{
float get() { return safe_cast<float>(obj); }
void set(float value) { obj = safe_cast<Object^>(value); }
}
/* Constant string */
property String^ str
{
String^ get() { return safe_cast<String^>(obj); }
void set(String^ value) { obj = safe_cast<Object^>(value); }
}
/* Expression or Expression Tree */
property ExprC^ exprR
{
ExprC^ get() { return safe_cast<ExprC^>(obj); }
void set(ExprC^ value) { obj = safe_cast<Object^>(value); }
}
/* List of expressions used for parameter lists */
property List<ExprC^>^ exprListR
{
List<ExprC^>^ get() { return safe_cast<List<ExprC^>^>(obj); }
void set(List<ExprC^>^ value) { obj = safe_cast<Object^>(value); }
}
/* List Of instructions */
property InstrListC^ instrListR
{
InstrListC^ get() { return safe_cast<InstrListC^>(obj); }
void set(InstrListC^ value) { obj = safe_cast<Object^>(value); }
}
/* Instruction with actual parameters */
property InstrC^ instrR
{
InstrC^ get() { return safe_cast<InstrC^>(obj); }
void set(InstrC^ value) { obj = safe_cast<Object^>(value); }
}
/* Run-time expression operator */
property OperatorC^ operatorR
{
OperatorC^ get() { return safe_cast<OperatorC^>(obj); }
void set(OperatorC^ value) { obj = safe_cast<Object^>(value); }
}
/* Instruction definition */
property InstrDefC^ instrDefR
{
InstrDefC^ get() { return safe_cast<InstrDefC^>(obj); }
void set(InstrDefC^ value) { obj = safe_cast<Object^>(value); }
}
/* Instruction definition */
property List<String^>^ stringListR
{
List<String^>^ get() { return safe_cast<List<String^>^>(obj); }
void set(List<String^>^ valueIR) { obj = safe_cast<Object^>(valueIR); }
}
};
Added 10-18-12 16:24 PDT
I have been able to reproduce this with a much simpler C++/CLI program.
void main()
{
array<int>^ intarray = gcnew array<int>(10);
intarray[0]=0;
intarray[1]=1;
intarray[2]=2;
intarray[3]=3;
intarray[4]=4;
intarray[5]=5;
}
Set breakpoint for last line then enter intarray[3] and intarray[4-1] into watch window. The [4-1] element displays 0.
Today I got the following response to posting this on Microsoft Connect last month:
Greetings from Microsoft Connect! This notification was generated for feedback item:768001
Visual Studio 2012 debugger fails computing index of managed C++/CLI array which you submitted at the
Microsoft Connect site. Hello Jon, Thanks for reporting this issue. We do not have a fix yet and have no current plans to fix this in the next release but will consider it for a future release. Thanks again. Marc Paine

How to handle errors

I was wondering what's the best way to handle errors in programmation (i'm coding in C). I'm a newbie in programmation and this is what i'm doing :
if( action1 was successfull )
{
[some code]
if (action 2 was successfull)
{
[some more code ..]
if (action 3 was successfull)
{
ETC ...
}
else
{
[Handle error3]
}
}
else
{
[handle error2]
}
}
else
{
[Handle error1]
}
But i think it's a bad habbit ... Can someone explain to me what's the most elegant way to do it ?
Thanks
I do it like this:
// more code
if (!action_1_succeeded())
{
handle_error();
return;
}
// more code
if (!action_2_succeeded())
{
handle_error();
return;
}
// more code
if (!action_3_succeeded())
{
handle_error();
return;
}
Usually, I will wrap up handle_error and return into a macro, so I just do check_error(some_statement());
Sometimes the code you show really is the best available option, but here are some alternatives.
Often you can structure your code like this instead:
if (action_1() failed) {
report error;
return -1;
}
if (action_2() failed) {
report error;
return -1;
}
/* etc */
This construct is why it's a common C idiom to return 0 on success or -1 on failure: it lets you write if (action_1() failed) as just if (action_1()).
If you need to do some cleanup before returning, and the clean-up actions nest neatly, this is another possibility:
if (action_1()) {
report error;
goto fail_a1;
}
if (action_2()) {
report error;
goto fail_a2;
}
if (action_3()) {
report error;
goto fail_a3;
}
/* etc */
return 0;
/* errors */
/* etc */
fail_a3:
clean up from a2;
fail_a2:
clean up from a1;
fail_a1:
return -1;
You are hereby licensed to use goto in order to do this.
You can create error-ids and associate an error handler with a particular type of error.
typedef struct _error {
void (*handler)(struct _error *);
int i_params;
char *c_params;
} error;
void nope(error * e) {}
void file_err(error * e) { exit(1); }
error handlers[200];
void check_error(int id) {
handlers[id].handler(&handlers[id]);
}
enum error { ER_SUCCESS, ER_FILE};
int main() {
handlers[ER_SUCCESS].handler = nope;
handlers[ER_FILE].handler = file_err;
check_error(action1());
return 0;
}

Resources